目錄
- 簡介:
- springboot redis啟動器
- redis配置:
- 工具類
簡介:
添加redis啟動器,配置redis相關配置,使用工具類緩存數據
封裝的方法有:緩存Object,list,set,map類型數據的方法、獲取指定key的value、判斷指定key是否有、設置key的有效期
用的類介紹:
TypeReference:
這個類是 Jackson 庫中的一個工具類,它允許你
保留 Java 類型的泛型信息
。由于Java 的類型擦除機制
,通常情況下在運行時無法獲取到泛型的實際類型參數
。但是通過使用
TypeReference
,你可以創建一個包裝類來保存泛型類型
,并將其傳遞給 Jackson 的方法(如 readValue() 和 writeValueAsString()),以便正確地處理泛型。
ObjectMapper:
可以序列化:將 Java 對象轉換為 JSON 字符串
也可以反序列化:將 JSON 字符串解析為 Java 對象
StringRedisTemplate:操作 Redis 數據庫中的字符串值,可以自動處理 Java 對象與 Redis 存儲的字符串之間的轉換,有很多的擴展方法。
springboot redis啟動器
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
redis配置:
spring:data:redis:host: localhostport: 6379jedis:pool:max-active: 8max-idle: 8min-idle: 0
工具類
RedisUtil.java
package com.ekgc.qy.util;import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;/*** @author Monk* @program: qyBootMaven* @description: redis緩存工具類* @create: 2023-12-11 16:56*/
@Component
public class RedisUtil {@Autowiredprivate StringRedisTemplate stringRedisTemplate;private static ObjectMapper objectMapper = new ObjectMapper();// 檢查給定的鍵是否存在于緩存中public boolean exists(String key) {return stringRedisTemplate.hasKey(key);}// 設置給定鍵的生存時間(以秒為單位)public void setExpireTime(String key, long seconds) {stringRedisTemplate.expire(key, seconds, TimeUnit.SECONDS);}// 緩存一個對象// 尖括號<T>聲明一個泛型public <T> void setObject(String key, T value) {try {String jsonValue = objectMapper.writeValueAsString(value);stringRedisTemplate.opsForValue().set(key, jsonValue);} catch (Exception e) {throw new RuntimeException("Failed to cache object", e);}}// 從緩存中獲取一個對象public <T> T getObject(String key, Class<T> clazz) {String jsonValue = stringRedisTemplate.opsForValue().get(key);if (jsonValue == null) {return null;}try {return objectMapper.readValue(jsonValue, clazz);} catch (Exception e) {throw new RuntimeException("Failed to parse cached object", e);}}// 緩存一個列表public <T> void setList(String key, List<T> list) {try {String jsonValue = objectMapper.writeValueAsString(list);stringRedisTemplate.opsForValue().set(key, jsonValue);} catch (Exception e) {throw new RuntimeException("Failed to cache list", e);}}// 從緩存中獲取一個列表public <T> List<T> getList(String key, Class<T> elementType) {String jsonValue = stringRedisTemplate.opsForValue().get(key);if (jsonValue == null) {return null;}try {TypeReference<List<T>> typeRef = new TypeReference<>() {};return objectMapper.readValue(jsonValue, typeRef);} catch (Exception e) {throw new RuntimeException("Failed to parse cached list", e);}}// 緩存一個集合public <T> void setSet(String key, Set<T> set) {try {String jsonValue = objectMapper.writeValueAsString(set);stringRedisTemplate.opsForValue().set(key, jsonValue);} catch (Exception e) {throw new RuntimeException("Failed to cache set", e);}}// 從緩存中獲取一個集合public <T> Set<T> getSet(String key, Class<T> elementType) {String jsonValue = stringRedisTemplate.opsForValue().get(key);if (jsonValue == null) {return null;}try {TypeReference<Set<T>> typeRef = new TypeReference<>() {};return objectMapper.readValue(jsonValue, typeRef);} catch (Exception e) {throw new RuntimeException("Failed to parse cached set", e);}}// 緩存一個映射public <K, V> void setMap(String key, Map<K, V> map) {try {String jsonValue = objectMapper.writeValueAsString(map);stringRedisTemplate.opsForValue().set(key, jsonValue);} catch (Exception e) {throw new RuntimeException("Failed to cache map", e);}}// 從緩存中獲取一個映射public <K, V> Map<K, V> getMap(String key, Class<K> keyType, Class<V> valueType) {String jsonValue = stringRedisTemplate.opsForValue().get(key);if (jsonValue == null) {return null;}try {TypeReference<Map<K, V>> typeRef = new TypeReference<>() {};return objectMapper.readValue(jsonValue, typeRef);} catch (Exception e) {throw new RuntimeException("Failed to parse cached map", e);}}
}