修复缓存报错问题

This commit is contained in:
Ranyunqiao
2026-06-11 13:07:28 +08:00
parent 8b47a8ab55
commit f655f06871

View File

@@ -1,6 +1,7 @@
package com.core.common.core.redis;
import com.core.common.exception.UtilException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
@@ -18,6 +19,7 @@ import java.util.concurrent.TimeUnit;
**/
@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
@Slf4j
public class RedisCache {
@Autowired
public RedisTemplate redisTemplate;
@@ -94,8 +96,20 @@ public class RedisCache {
* @return 缓存键值对应的数据
*/
public <T> T getCacheObject(final String key) {
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
try {
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
} catch (Exception e) {
log.error("Redis获取对象异常, key: {}, 错误信息: {}", key, e.getMessage());
// 如果发生序列化等异常,可能是旧版本数据或损坏数据,直接删除以防止程序崩溃
try {
redisTemplate.delete(key);
log.info("已自动清理损坏的缓存Key: {}", key);
} catch (Exception ex) {
log.error("自动清理损坏缓存失败, key: {}", key, ex);
}
return null;
}
}
/**