我們項目中經常用到Redis
,而且存的值很多是json
格式。所以這邊我就自定義了RedisTemplate
來修改Redis
的序列化方式。而且封裝了常用的Redis
的操作。
一、創建自定義配置類RedisConfig
package com.riemann.springbootdemo.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author riemann
* @date 2020/03/30 20:54
*/
@Configuration
public class RedisConfig {
/**
* @SuppressWarnings 該批註的作用是給編譯器一條指令,告訴它對被批註的代碼元素內部的某些警告保持靜默
*
* 關鍵字 用途
* deprecation 使用了不贊成使用的類或方法時的警告
* unchecked 執行了未檢查的轉換時的警告,例如當使用集合時沒有用泛型 (Generics) 來指定集合保存的類型。
* fallthrough 當 Switch 程序塊直接通往下一種情況而沒有 Break 時的警告。
* path 在類路徑、源文件路徑等中有不存在的路徑時的警告。
* serial 當在可序列化的類上缺少 serialVersionUID 定義時的警告。
* finally 任何 finally 子句不能正常完成時的警告。
* all 關於以上所有情況的警告。
*/
// 注入 Spring 容器中,忽略所有警告
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key 採用 String 的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash 的 key 也採用 String 的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value 的序列化方式採用 JSON
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash value 的序列化方式也採用 JSON
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
二、Redis的常用操作封裝RedisUtil
package com.riemann.springbootdemo.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @author riemann
* @date 2020/03/30 21:59
*/
@Component
public class RedisUtil {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 指定緩存失效時間
* @param key 鍵
* @param time 時間(秒)
* @return true / false
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根據 key 獲取過期時間
* @param key 鍵
* @return
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 判斷 key 是否存在
* @param key 鍵
* @return true / false
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 刪除緩存
* @SuppressWarnings("unchecked") 忽略類型轉換警告
* @param key 鍵(一個或者多個)
*/
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
// 傳入一個 Collection<String> 集合
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
// ============================== String ==============================
/**
* 普通緩存獲取
* @param key 鍵
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通緩存放入
* @param key 鍵
* @param value 值
* @return true / false
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通緩存放入並設置時間
* @param key 鍵
* @param value 值
* @param time 時間(秒),如果 time < 0 則設置無限時間
* @return true / false
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 遞增
* @param key 鍵
* @param delta 遞增大小
* @return
*/
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("遞增因子必須大於 0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 遞減
* @param key 鍵
* @param delta 遞減大小
* @return
*/
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("遞減因子必須大於 0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
// ============================== Map ==============================
/**
* HashGet
* @param key 鍵(no null)
* @param item 項(no null)
* @return 值
*/
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/**
* 獲取 key 對應的 map
* @param key 鍵(no null)
* @return 對應的多個鍵值
*/
public Map<Object, Object> hmget(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* HashSet
* @param key 鍵
* @param map 值
* @return true / false
*/
public boolean hmset(String key, Map<Object, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* HashSet 並設置時間
* @param key 鍵
* @param map 值
* @param time 時間
* @return true / false
*/
public boolean hmset(String key, Map<Object, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一張 Hash表 中放入數據,如不存在則創建
* @param key 鍵
* @param item 項
* @param value 值
* @return true / false
*/
public boolean hset(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一張 Hash表 中放入數據,並設置時間,如不存在則創建
* @param key 鍵
* @param item 項
* @param value 值
* @param time 時間(如果原來的 Hash表 設置了時間,這裏會覆蓋)
* @return true / false
*/
public boolean hset(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 刪除 Hash表 中的值
* @param key 鍵
* @param item 項(可以多個,no null)
*/
public void hdel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
/**
* 判斷 Hash表 中是否有該鍵的值
* @param key 鍵(no null)
* @param item 值(no null)
* @return true / false
*/
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* Hash遞增,如果不存在則創建一個,並把新增的值返回
* @param key 鍵
* @param item 項
* @param by 遞增大小 > 0
* @return
*/
public Double hincr(String key, String item, Double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/**
* Hash遞減
* @param key 鍵
* @param item 項
* @param by 遞減大小
* @return
*/
public Double hdecr(String key, String item, Double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
// ============================== Set ==============================
/**
* 根據 key 獲取 set 中的所有值
* @param key 鍵
* @return 值
*/
public Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 從鍵爲 key 的 set 中,根據 value 查詢是否存在
* @param key 鍵
* @param value 值
* @return true / false
*/
public boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將數據放入 set緩存
* @param key 鍵值
* @param values 值(可以多個)
* @return 成功個數
*/
public long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 將數據放入 set緩存,並設置時間
* @param key 鍵
* @param time 時間
* @param values 值(可以多個)
* @return 成功放入個數
*/
public long sSet(String key, long time, Object... values) {
try {
long count = redisTemplate.opsForSet().add(key, values);
if (time > 0) {
expire(key, time);
}
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 獲取 set緩存的長度
* @param key 鍵
* @return 長度
*/
public long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 移除 set緩存中,值爲 value 的
* @param key 鍵
* @param values 值
* @return 成功移除個數
*/
public long setRemove(String key, Object... values) {
try {
return redisTemplate.opsForSet().remove(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
// ============================== List ==============================
/**
* 獲取 list緩存的內容
* @param key 鍵
* @param start 開始
* @param end 結束(0 到 -1 代表所有值)
* @return
*/
public List<Object> lGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 獲取 list緩存的長度
* @param key 鍵
* @return 長度
*/
public long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 根據索引 index 獲取鍵爲 key 的 list 中的元素
* @param key 鍵
* @param index 索引
* 當 index >= 0 時 {0:表頭, 1:第二個元素}
* 當 index < 0 時 {-1:表尾, -2:倒數第二個元素}
* @return 值
*/
public Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 將值 value 插入鍵爲 key 的 list 中,如果 list 不存在則創建空 list
* @param key 鍵
* @param value 值
* @return true / false
*/
public boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將值 value 插入鍵爲 key 的 list 中,並設置時間
* @param key 鍵
* @param value 值
* @param time 時間
* @return true / false
*/
public boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將 values 插入鍵爲 key 的 list 中
* @param key 鍵
* @param values 值
* @return true / false
*/
public boolean lSetList(String key, List<Object> values) {
try {
redisTemplate.opsForList().rightPushAll(key, values);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將 values 插入鍵爲 key 的 list 中,並設置時間
* @param key 鍵
* @param values 值
* @param time 時間
* @return true / false
*/
public boolean lSetList(String key, List<Object> values, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, values);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根據索引 index 修改鍵爲 key 的值
* @param key 鍵
* @param index 索引
* @param value 值
* @return true / false
*/
public boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 在鍵爲 key 的 list 中刪除值爲 value 的元素
* @param key 鍵
* @param count 如果 count == 0 則刪除 list 中所有值爲 value 的元素
* 如果 count > 0 則刪除 list 中最左邊那個值爲 value 的元素
* 如果 count < 0 則刪除 list 中最右邊那個值爲 value 的元素
* @param value
* @return
*/
public long lRemove(String key, long count, Object value) {
try {
return redisTemplate.opsForList().remove(key, count, value);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
}