使用SpringBoot中整合Redis
SpringBoot中整合Redis
本次,我們以IDEA + SpringBoot作為 Java中整合Redis的使用 的測(cè)試環(huán)境,如果對(duì)創(chuàng)建SpringBoot項(xiàng)目有不清楚的地方,可以參考這篇文章:使用Idea創(chuàng)建我的第一個(gè)SpringBoot項(xiàng)目
首先,我們需要導(dǎo)入Redis的maven依賴
<!-- Redis的maven依賴包 --> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>org.springframework.boot</groupId> ?? ??? ??? ?<artifactId>spring-boot-starter-data-redis</artifactId> ?? ??? ?</dependency>
其次,我們需要在配置文件中配置你的Redis配置信息,我使用的是 .yml文件格式
# redis配置
spring:
redis:
# r服務(wù)器地址
host: 127.0.0.1
# 服務(wù)器端口
port: 6379
# 數(shù)據(jù)庫索引(默認(rèn)0)
database: 0
jedis:
pool:
# 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
max-active: 50
# 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制)
max-wait: 3000ms
# 連接池中的最大空閑連接數(shù)
max-idle: 20
# 連接池中的最小空閑連接數(shù)
min-idle: 2
# 連接超時(shí)時(shí)間(毫秒)
timeout: 5000ms然后,我們需要?jiǎng)?chuàng)建一個(gè)RedisUtil來對(duì)Redis數(shù)據(jù)庫進(jìn)行操作
package com.zyxx.test.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
/**
* @ClassName RedisUtil
* @Author Lizhou
* @Date 2019-08-03 17:29:29
* @Version 1.0
**/
@Component
public class RedisUtil {
@Autowired
private RedisTemplate<String, String> template;
/**
* 讀取數(shù)據(jù)
*
* @param key
* @return
*/
public String get(final String key) {
return template.opsForValue().get(key);
}
/**
* 寫入數(shù)據(jù)
*/
public boolean set(final String key, String value) {
boolean res = false;
try {
template.opsForValue().set(key, value);
res = true;
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
/**
* 根據(jù)key更新數(shù)據(jù)
*/
public boolean update(final String key, String value) {
boolean res = false;
try {
template.opsForValue().getAndSet(key, value);
res = true;
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
/**
* 根據(jù)key刪除數(shù)據(jù)
*/
public boolean del(final String key) {
boolean res = false;
try {
template.delete(key);
res = true;
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
/**
* 是否存在key
*/
public boolean hasKey(final String key) {
boolean res = false;
try {
res = template.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
/**
* 給指定的key設(shè)置存活時(shí)間
* 默認(rèn)為-1,表示永久不失效
*/
public boolean setExpire(final String key, long seconds) {
boolean res = false;
try {
if (0 < seconds) {
res = template.expire(key, seconds, TimeUnit.SECONDS);
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
/**
* 獲取指定key的剩余存活時(shí)間
* 默認(rèn)為-1,表示永久不失效,-2表示該key不存在
*/
public long getExpire(final String key) {
long res = 0;
try {
res = template.getExpire(key, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
/**
* 移除指定key的有效時(shí)間
* 當(dāng)key的有效時(shí)間為-1即永久不失效和當(dāng)key不存在時(shí)返回false,否則返回true
*/
public boolean persist(final String key) {
boolean res = false;
try {
res = template.persist(key);
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
}
最后,我們可以使用單元測(cè)試來檢測(cè)我們?cè)赗edisUtil中寫的操作Redis數(shù)據(jù)庫的方法
package com.zyxx.test;
import com.zyxx.test.utils.RedisUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestApplicationTest {
@Resource
private RedisUtil redisUtil;
@Test
public void setRedis() {
boolean res = redisUtil.set("jay", "周杰倫 - 《以父之名》");
System.out.println(res);
}
@Test
public void getRedis() {
String res = redisUtil.get("jay");
System.out.println(res);
}
@Test
public void updateRedis() {
boolean res = redisUtil.update("jay", "周杰倫 - 《夜的第七章》");
System.out.println(res);
}
@Test
public void delRedis() {
boolean res = redisUtil.del("jay");
System.out.println(res);
}
@Test
public void hasKey() {
boolean res = redisUtil.hasKey("jay");
System.out.println(res);
}
@Test
public void expire() {
boolean res = redisUtil.setExpire("jay", 100);
System.out.println(res);
}
@Test
public void getExpire() {
long res = redisUtil.getExpire("jay");
System.out.println(res);
}
@Test
public void persist() {
boolean res = redisUtil.persist("jay");
System.out.println(res);
}
}
推薦使用Redis客戶端(redis-desktop-manager)來查看Redis數(shù)據(jù)庫中的數(shù)據(jù)
至此,我們?cè)谌粘m?xiàng)目中整合Redis的基本使用操作就完成了,但在實(shí)際項(xiàng)目中,可能會(huì)涉及到更復(fù)雜的用法,可以根據(jù)你的業(yè)務(wù)需求調(diào)整Redis的使用即可。
SpringBoot整合Redis改不了database問題
關(guān)于學(xué)習(xí)redis寫的yaml文件,里面的
redis: ? database: 15 ? host: 127.0.0.1 ? port: 6379 ? password: ? timeout: 3000ms # 連接超時(shí)時(shí)間(毫秒)
但是springboot自動(dòng)裝配一直是database為0
最后發(fā)現(xiàn)是沒有找到我配置的redis,默認(rèn)是用的自動(dòng)裝配的默認(rèn)地址(所以我一直以為我的yaml寫對(duì)了,但是沒用寫對(duì),因?yàn)橐恢睕]有取到我的值)
自動(dòng)裝配的就是
database=0,port=6379;無密碼,host=localhost,
就是因?yàn)檫@個(gè)原因,我寫的數(shù)據(jù)能進(jìn)入我的本地redis中,所以我一直沒找到問題,因?yàn)槲乙彩褂玫谋镜氐膔edis測(cè)試的,一直找不到為什么改不了database。
最后發(fā)現(xiàn)是我的yaml文件的redis寫錯(cuò)了,把redis寫到log下面,并且歸屬于log。
最后該成歸屬于spring下面就可以了。
注意:一定要注意yaml的書寫規(guī)范和上下級(jí)的關(guān)系。
深刻的教訓(xùn),我在網(wǎng)上查了很多,也沒找到對(duì)應(yīng)的辦法,幸好沒有,不然的話,我的代碼會(huì)被改的亂七八糟的,花了一下午才發(fā)現(xiàn)是自己yaml寫錯(cuò)了。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot整合Redis之編寫RedisConfig
- SpringBoot整合Redis將對(duì)象寫入redis的實(shí)現(xiàn)
- Spring?boot?整合?Redisson實(shí)現(xiàn)分布式鎖并驗(yàn)證功能
- SpringBoot整合Shiro和Redis的示例代碼
- spring boot整合redis主從sentinel方式
- SpringBoot整合Mysql和Redis的詳細(xì)過程
- SpringBoot整合Redis實(shí)現(xiàn)訪問量統(tǒng)計(jì)的示例代碼
- springboot整合redis實(shí)現(xiàn)發(fā)送郵箱并驗(yàn)證
- Spring Boot示例代碼整合Redis詳解
相關(guān)文章
WebUploader客戶端批量上傳圖片 后臺(tái)使用springMVC
這篇文章主要為大家詳細(xì)介紹了WebUploader客戶端批量上傳圖片,后臺(tái)使用springMVC接收實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Java 對(duì)HashMap進(jìn)行排序的三種常見方法
這篇文章主要介紹了Java 對(duì)HashMap進(jìn)行排序的三種常見方法,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-10-10
解析Linux系統(tǒng)中JVM內(nèi)存2GB上限的詳解
本篇文章是對(duì)Linux系統(tǒng)中JVM內(nèi)存2GB上限進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Spring boot項(xiàng)目整合WebSocket方法
這篇文章主要介紹了WebSocket使用Spring boot整合方法,需要繼承webSocketHandler類,重寫幾個(gè)方法就可以了,具體實(shí)例代碼跟隨小編一起看看吧2021-09-09
Java框架Struts2實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了Java框架Struts2實(shí)現(xiàn)圖片上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
spring整合redis消息監(jiān)聽通知使用的實(shí)現(xiàn)示例
在電商系統(tǒng)中,秒殺,搶購,紅包優(yōu)惠卷等操作,一般都會(huì)設(shè)置時(shí)間限制,本文主要介紹了spring整合redis消息監(jiān)聽通知使用,具有一定的參考價(jià)值,感興趣的可以了解一下2021-12-12
java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):單,雙向鏈表
這篇文章主要介紹了Java的數(shù)據(jù)解構(gòu)基礎(chǔ),希望對(duì)廣大的程序愛好者有所幫助,同時(shí)祝大家有一個(gè)好成績(jī),需要的朋友可以參考下,希望能給你帶來幫助2021-07-07

