refactor: 从CacheConfig注解中移除keyGenerator,优化Redis配置
This commit is contained in:
parent
f930c9ac4b
commit
3cbab55eb8
@ -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()));
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user