Redis 在 SpringBoot 中的使用
阿新 • • 發佈:2019-12-31
Redis 支援多種 java 客戶端,支援列表參考官網
Jedis 是redis官方推薦的java客戶端
在SpringBoot 中使用 Redis 簡單分為三步
- 引入依賴
- 配置 redis
- 使用 redis
引入依賴
springboot 2.0.0.RELEASE 之前,data-redis 底層客戶端為 jedis
從springboot 2.0.0.RELEASE 開始底層區分兩個不同的實現,jedis及lettuce,預設採用 lettuce
<!-- redis依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
複製程式碼
配置
底層 redis 客戶端的變更,影響到了上次配置的改變,redis 因此也分為兩種
springboot 2.0.0.RELEASE 之前配置
spring.redis.database=0
spring.redis.host=192.168.99.100
spring.redis.port=6379
# redis伺服器的登入密碼 沒有可以不配置
#spring.redis.password=
spring.redis.pool.max-active=8
spring.redis.pool.max-idle=8
spring.redis.pool.max-wait=-1
spring.redis.pool.min-idle=0
# redis 服務名稱
#spring.redis.sentinel.master=
# 主機列表,格式為 host:port, 多個用逗號分隔
#spring.redis.sentinel.nodes=
spring.redis.timeout=10
複製程式碼
springboot 2.0.0.RELEASE 之後配置
spring.redis.database=0
spring.redis.host=192.168.99.100
spring.redis.port=6379
# redis伺服器的登入密碼 沒有可以不配置
#spring.redis.password=
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.min-idle=0
# redis 服務名稱
#spring.redis.sentinel.master=
# 主機列表,格式為 host:port, 多個用逗號分隔
#spring.redis.sentinel.nodes=
spring.redis.timeout=100ms
複製程式碼
主要變動
springboot 2.0.0.RELEASE 之後 pool 改為 lettuce.pool
公共配置 spring.redis.timeout 的引數改為 Duration 型別,需要增加時間單位引數 如毫秒ms 秒s
使用
redis 中 一般有兩種資料型別使用最多 String 和 hash,下面以這兩種資料型別為例 簡單演示下 redis 在 Springboot 中的使用
string資料型別
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Service
public class RedisServiceImpl {
@Autowired
private RedisTemplate redisTemplate;
/**
* 新增 Redis術語中 string 型別的資料
*
* @param key String型別
* @param value 任意型別,但必須實現序列化介面
* @return
*/
public boolean set(final String key,Object value) {
boolean result = false;
try {
// 建立對簡單值(Redis術語中的string型別)執行操作的物件
ValueOperations<Serializable,Object> operations = redisTemplate.opsForValue();
operations.set(key,value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 新增 Redis術語中 string 型別的資料,並設定超時
*
* @param key String型別
* @param value 任意型別,但必須實現序列化介面
* @param expireTime 單位分鐘
* @return
*/
public boolean set(final String key,Object value,Long expireTime) {
boolean result = false;
try {
// 建立對簡單值(Redis術語中的string型別)執行操作的物件
ValueOperations<Serializable,value);
redisTemplate.expire(key,expireTime,TimeUnit.MINUTES);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 判斷 key 是否存在
*
* @param key
* @return
*/
public boolean existsKey(final String key) {
return redisTemplate.hasKey(key);
}
/**
* 根據( Redis 術語中 string 型別的) key 獲取值,如果出現異常則返回null
*
* @param key
* @param type
* @param <T> 存入 redis 時的型別
* @return
*/
public <T> T getValue(final String key,Class<T> type) {
Object result = null;
ValueOperations<Serializable,Object> operations = redisTemplate.opsForValue();
try {
result = operations.get(key);
if (result == null) {
return null;
}
// 將 Object 型別強轉成 type 對應的型別
return type.cast(result);
} catch (Exception e) {
return null;
}
}
/**
* 刪除對應的value
*
* @param key
*/
public void removeKey(final String key) {
// 檢查 key 是否存在
if (existsKey(key)) {
redisTemplate.delete(key);
}
}
/**
* 批量刪除對應的value
*
* @param keys
*/
public void remove(final String... keys) {
for (String key : keys) {
removeKey(key);
}
}
/**
* 批量刪除key
*
* @param pattern
*/
public void removePattern(final String pattern) {
// 獲取所有匹配的鍵
Set<Serializable> keys = redisTemplate.keys(pattern);
if (keys != null && keys.size() > 0) {
redisTemplate.delete(keys);
}
}
}
複製程式碼
hash 資料型別
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.io.Serializable;
import java.util.Set;
@Service
public class RedisServiceImpl {
@Autowired
private RedisTemplate redisTemplate;
/**
* 新增 Redis 術語中 hash 型別的資料
*
* @param key
* @param hashKey string 型別
* @param value 任意型別,但必須實現序列化介面
* @return
*/
public boolean setHash(String key,String hashKey,Object value) {
boolean result = false;
try {
// 建立對 hash 值(Redis術語中的 hash 型別)執行操作的物件
HashOperations<String,String,Object> hashOperations = redisTemplate.opsForHash();
hashOperations.put(key,hashKey,value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 判斷redis key中是否有hashKey對應的value
*
* @param key
* @return
*/
public boolean existsHashKey(String key,String hashKey) {
return redisTemplate.opsForHash().hasKey(key,hashKey);
}
/**
* 根據key,hashKey讀取value
*
* @param key
* @return
*/
public Object getHashValue(final String key,final String hashKey) {
// 建立對 hash 值(Redis術語中的 hash 型別)執行操作的物件
HashOperations<String,Object> hashOperations = redisTemplate.opsForHash();
try {
return hashOperations.get(key,hashKey);
} catch (Exception e) {
return null;
}
}
/**
* 判斷 key 是否存在
*
* @param key
* @return
*/
public boolean existsKey(final String key) {
return redisTemplate.hasKey(key);
}
/**
* 刪除對應的value
*
* @param key
*/
public void removeKey(final String key) {
// 檢查 key 是否存在
if (existsKey(key)) {
redisTemplate.delete(key);
}
}
/**
* 批量刪除對應的value
*
* @param keys
*/
public void remove(final String... keys) {
for (String key : keys) {
removeKey(key);
}
}
/**
* 批量刪除key
*
* @param pattern
*/
public void removePattern(final String pattern) {
// 獲取所有匹配的鍵
Set<Serializable> keys = redisTemplate.keys(pattern);
if (keys != null && keys.size() > 0) {
redisTemplate.delete(keys);
}
}
/**
* 刪除對應的key的指定hashKey
*
* @param key
*/
public void removeHashKey(final String key,final String hashKey) {
if (existsHashKey(key,hashKey)) {
redisTemplate.opsForHash().delete(key,hashKey);
;
}
}
}
複製程式碼