使用SpringBoot中整合Redis
SpringBoot中整合Redis
本次,我們以IDEA + SpringBoot作為 Java中整合Redis的使用 的測試環(huán)境,如果對創(chuàng)建SpringBoot項目有不清楚的地方,可以參考這篇文章:使用Idea創(chuàng)建我的第一個SpringBoot項目
首先,我們需要導入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ù)庫索引(默認0)
database: 0
jedis:
pool:
# 連接池最大連接數(shù)(使用負值表示沒有限制)
max-active: 50
# 連接池最大阻塞等待時間(使用負值表示沒有限制)
max-wait: 3000ms
# 連接池中的最大空閑連接數(shù)
max-idle: 20
# 連接池中的最小空閑連接數(shù)
min-idle: 2
# 連接超時時間(毫秒)
timeout: 5000ms然后,我們需要創(chuàng)建一個RedisUtil來對Redis數(shù)據(jù)庫進行操作
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è)置存活時間
* 默認為-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的剩余存活時間
* 默認為-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的有效時間
* 當key的有效時間為-1即永久不失效和當key不存在時返回false,否則返回true
*/
public boolean persist(final String key) {
boolean res = false;
try {
res = template.persist(key);
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
}
最后,我們可以使用單元測試來檢測我們在RedisUtil中寫的操作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ù)
至此,我們在日常項目中整合Redis的基本使用操作就完成了,但在實際項目中,可能會涉及到更復雜的用法,可以根據(jù)你的業(yè)務(wù)需求調(diào)整Redis的使用即可。
SpringBoot整合Redis改不了database問題
關(guān)于學習redis寫的yaml文件,里面的
redis: ? database: 15 ? host: 127.0.0.1 ? port: 6379 ? password: ? timeout: 3000ms # 連接超時時間(毫秒)
但是springboot自動裝配一直是database為0
最后發(fā)現(xiàn)是沒有找到我配置的redis,默認是用的自動裝配的默認地址(所以我一直以為我的yaml寫對了,但是沒用寫對,因為一直沒有取到我的值)
自動裝配的就是
database=0,port=6379;無密碼,host=localhost,
就是因為這個原因,我寫的數(shù)據(jù)能進入我的本地redis中,所以我一直沒找到問題,因為我也使用的本地的redis測試的,一直找不到為什么改不了database。
最后發(fā)現(xiàn)是我的yaml文件的redis寫錯了,把redis寫到log下面,并且歸屬于log。
最后該成歸屬于spring下面就可以了。
注意:一定要注意yaml的書寫規(guī)范和上下級的關(guān)系。
深刻的教訓,我在網(wǎng)上查了很多,也沒找到對應(yīng)的辦法,幸好沒有,不然的話,我的代碼會被改的亂七八糟的,花了一下午才發(fā)現(xiàn)是自己yaml寫錯了。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
WebUploader客戶端批量上傳圖片 后臺使用springMVC
這篇文章主要為大家詳細介紹了WebUploader客戶端批量上傳圖片,后臺使用springMVC接收實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09
解析Linux系統(tǒng)中JVM內(nèi)存2GB上限的詳解
本篇文章是對Linux系統(tǒng)中JVM內(nèi)存2GB上限進行了詳細的分析介紹,需要的朋友參考下2013-05-05
spring整合redis消息監(jiān)聽通知使用的實現(xiàn)示例
在電商系統(tǒng)中,秒殺,搶購,紅包優(yōu)惠卷等操作,一般都會設(shè)置時間限制,本文主要介紹了spring整合redis消息監(jiān)聽通知使用,具有一定的參考價值,感興趣的可以了解一下2021-12-12
java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):單,雙向鏈表
這篇文章主要介紹了Java的數(shù)據(jù)解構(gòu)基礎(chǔ),希望對廣大的程序愛好者有所幫助,同時祝大家有一個好成績,需要的朋友可以參考下,希望能給你帶來幫助2021-07-07

