redis 缓存问题

This commit is contained in:
2024-12-13 15:25:15 +08:00
parent 8879666375
commit fcdf71a9b0
2 changed files with 32 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
package com.sqx.common.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.sqx.modules.redisService.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -9,6 +10,7 @@ import org.springframework.stereotype.Component;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
@@ -43,7 +45,30 @@ public class RedisUtils {
* @param clazz 返回类型
* @param method RedisService调用的方法名 如果数据不存在会执行该调用方法
*/
public <T> T getDate(String key, Class<T> clazz,String method) {
public <T> List<T> getListData(String key, Class<T> clazz,String method) {
String jsonStr = getDate(key, method);
ObjectMapper objectMapper = new ObjectMapper();
try {
// 将JSON字符串转换为List<T>
return objectMapper.readValue(jsonStr, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取缓存里的数据 如果不存在 则插入 并返回
* @param key redis Key
* @param clazz 返回类型
* @param method RedisService调用的方法名 如果数据不存在会执行该调用方法
*/
public <T> T getObjectDate(String key, Class<T> clazz,String method) {
String jsonStr = getDate(key, method);
return this.fromJson(jsonStr, clazz);
}
public String getDate(String key, String method) {
if (!this.hasKey(key)) {
try {
// 获取Lookup对象
@@ -61,7 +86,7 @@ public class RedisUtils {
throw new RuntimeException(e);
}
}
return this.get(key, clazz);
return this.get(key);
}
public boolean hasKey(String key) {