自定义RedisCacheConfig
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class RedisCacheConfig {
/**
* MONTH_TIMEOUT 指定 key 过期时间 1个月
*/
private static int MONTH_TIMEOUT = 2592000;
/**
* DAY_TIMEOUT 指定 key 过期时间 1天
*/
private static int DAY_TIMEOUT = 86400;
/**
* DEFAULT_TIMEOUT_HOUR 默认 1小时过期
*/
private static int DEFAULT_TIMEOUT_HOUR = 3600;
@Bean
public KeyGenerator simpleKeyGenerator() {
return (o, method, objects) -> {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(o.getClass().getSimpleName());
stringBuilder.append(".");
stringBuilder.append(method.getName());
stringBuilder.append("[");
for (Object obj : objects) {
stringBuilder.append(obj.toString());
}
stringBuilder.append("]");
return stringBuilder.toString();
};
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
return new RedisCacheManager(
RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
// 默认策略,未配置的 key 会使用这个
this.getRedisCacheConfigurationWithTtl(DEFAULT_TIMEOUT_HOUR),
// 指定 key 策略
this.getRedisCacheConfigurationMap()
);
}
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
redisCacheConfigurationMap.put("touchpoint:zone:getZoneList", this.getRedisCacheConfigurationWithTtl(MONTH_TIMEOUT));
redisCacheConfigurationMap.put("touchpoint:zone:getSublevelAreaInfoByCode", this.getRedisCacheConfigurationWithTtl(MONTH_TIMEOUT));
redisCacheConfigurationMap.put("touchpoint:dict:getServiceDict", this.getRedisCacheConfigurationWithTtl(MONTH_TIMEOUT));
redisCacheConfigurationMap.put("touchpoint:org:getOrgListByUserId", this.getRedisCacheConfigurationWithTtl(DAY_TIMEOUT));
return redisCacheConfigurationMap;
}
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
RedisSerializationContext
.SerializationPair
.fromSerializer(jackson2JsonRedisSerializer)
).entryTtl(Duration.ofSeconds(seconds));
return redisCacheConfiguration;
}
}
常用命令
缓存
@Cacheable(cacheNames = "touchpoint:dict:getServiceDict", key = "#param.moudle+'-'+#param.dictCode")
将对应的cacheName全部删除
@CacheRemoveAll(cacheName = "touchpoint:dict:getServiceDict")
删除指定可以
@CacheEvict(cacheNames = "touchpoint:dict:getServiceDict", key = "#param.moudle+'-'+#param.dictCode")