refactor: 从CacheConfig注解中移除keyGenerator,优化Redis配置

This commit is contained in:
Jie Zheng 2025-01-25 10:19:12 +08:00
parent f930c9ac4b
commit 3cbab55eb8
6 changed files with 17 additions and 15 deletions

View File

@ -19,8 +19,9 @@ import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONFactory;
import com.alibaba.fastjson2.JSONWriter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.MurmurHash3;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.KeyGenerator;
@ -47,7 +48,10 @@ import java.util.Map;
@Slf4j
@Configuration
@EnableCaching
public class RedisConfiguration {
public class RedisConfiguration extends CachingConfigurerSupport {
// 自动识别json对象白名单配置仅允许解析的包名范围越小越安全
private static final String[] WHITELIST_STR = {"me.zhengjie" };
/**
* 设置 redis 数据默认过期时间默认2小时
@ -71,7 +75,9 @@ public class RedisConfiguration {
template.setValueSerializer(fastJsonRedisSerializer);
template.setHashValueSerializer(fastJsonRedisSerializer);
// 设置fastJson的序列化白名单
JSONFactory.getDefaultObjectReaderProvider().addAutoTypeAccept("me.zhengjie");
for (String pack : WHITELIST_STR) {
JSONFactory.getDefaultObjectReaderProvider().addAutoTypeAccept(pack);
}
// key的序列化采用StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
@ -93,9 +99,7 @@ public class RedisConfiguration {
}
/**
* 自定义缓存key生成策略需要在缓存注解中使用keyGenerator才会生效
* 默认是使用SimpleKeyGenerator生成的key
* 继承 CachingConfigurerSupport 才会默认生效这个生成器暂时没找到其他方式默认生效如果有请PR谢谢
* 自定义缓存key生成策略
*/
@Bean
public KeyGenerator keyGenerator() {
@ -114,8 +118,8 @@ public class RedisConfiguration {
}
// 转为JSON字符串
String jsonString = JSON.toJSONString(container);
// 做SHA256 Hash计算得到一个SHA256摘要作为Key
return DigestUtils.sha256Hex(jsonString);
// 使用 MurmurHash 生成 hash
return Integer.toHexString(MurmurHash3.hash32x86(jsonString.getBytes()));
};
}

View File

@ -20,14 +20,12 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.modules.system.domain.Dict;
import me.zhengjie.modules.system.domain.Job;
import me.zhengjie.modules.system.mapper.UserMapper;
import me.zhengjie.modules.system.domain.dto.JobQueryCriteria;
import me.zhengjie.utils.*;
import me.zhengjie.modules.system.mapper.JobMapper;
import me.zhengjie.modules.system.service.JobService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;

View File

@ -38,7 +38,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "aliPay", keyGenerator = "keyGenerator")
@CacheConfig(cacheNames = "aliPay")
public class AliPayServiceImpl extends ServiceImpl<AliPayConfigMapper, AlipayConfig> implements AliPayService {
@Override

View File

@ -37,7 +37,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "email", keyGenerator = "keyGenerator")
@CacheConfig(cacheNames = "email")
public class EmailServiceImpl extends ServiceImpl<EmailConfigMapper, EmailConfig> implements EmailService {
@Override

View File

@ -33,7 +33,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "qiNiu", keyGenerator = "keyGenerator")
@CacheConfig(cacheNames = "qiNiu")
public class QiNiuConfigServiceImpl extends ServiceImpl<QiniuConfigMapper, QiniuConfig> implements QiNiuConfigService {
@Override

View File

@ -52,7 +52,7 @@ import java.util.*;
*/
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "qiNiu", keyGenerator = "keyGenerator")
@CacheConfig(cacheNames = "qiNiu")
public class QiniuContentServiceImpl extends ServiceImpl<QiniuContentMapper, QiniuContent> implements QiniuContentService {
private final QiniuContentMapper qiniuContentMapper;
@ -104,7 +104,7 @@ public class QiniuContentServiceImpl extends ServiceImpl<QiniuContentMapper, Qin
}
return content;
} catch (Exception e) {
throw new BadRequestException(e.getMessage());
throw new BadRequestException(e.getMessage());
}
}