7

SpringBoot 2.x 以后,@Cacheable, Redis-cahce 的配置变动比较大

网上找了点资料,目前我的配置是

@Bean
    public RedisCacheConfiguration redisCacheConfiguration() {
        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.ofMinutes(30));

        return redisCacheConfiguration;
    }

使用上述代码后,可以成功缓存,但不能对key指定缓存时间,

clipboard.png

如图,两个 key UserInfoList 和 key UserInfoListAnother 都是默认的30分钟

2.X以后无法再使用 RedisCacheManager rcm = new RedisCacheManager(redisTemplate) 构造方法来设置缓存时间,请问该如何配置?

2018-06-06 提问
3 个回答
7

已采纳

google 了一晚上,终于找到了比较满意的方法,下面是整个的 RedisCacheConfig 文件

@Configuration
public class RedisCacheConfig {

    @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),
            this.getRedisCacheConfigurationWithTtl(600), // 默认策略,未配置的 key 会使用这个
            this.getRedisCacheConfigurationMap() // 指定 key 策略
        );
    }

    private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
        Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
        redisCacheConfigurationMap.put("UserInfoList", this.getRedisCacheConfigurationWithTtl(3000));
        redisCacheConfigurationMap.put("UserInfoListAnother", this.getRedisCacheConfigurationWithTtl(18000));

        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;
    }
}

要指定 key 的过期时间,只要在getRedisCacheConfigurationMap方法中添加就可以。
然后只需要 @Cacheable 就可以把数据存入 redis

    @Cacheable(value = "UserInfoList", keyGenerator = "simpleKeyGenerator") // 3000秒
    @Cacheable(value = "UserInfoListAnother", keyGenerator = "simpleKeyGenerator") // 18000秒
    @Cacheable(value = "DefaultKeyTest", keyGenerator = "simpleKeyGenerator") // 600秒,未指定的key,使用默认策略
    
0

使用Spring Boot了当然是使用Redis的自动装配了,只需要引入redis-starter即可开箱用
`

redisTemplate.opsForValue().set(key, value, expires, timeUnit)
   

`

补充一下你的Bean

@Bean
public RedisCacheConfiguration redisCacheConfiguration(RedisTemplate<String, Object> template, CacheStrategy stategy) {

    redisCacheManager.setExpires(strategy.getExpiresMap);
    return redisCacheConfiguration;
}

然后配置CacheStrategy类

public static final String KEY = "_TEST";

public static Long CACHE_TIME = 10000L;
private Map<String, Long> expiresMap = null;

@PostConstruct
public void init(){
    expiresMap = Maps.newHashMap();
    expiresMap.put(KEY, CACHE_TIME);
}

public Map<String, Long> getExpiresMap(){
    return this.expiresMap;
}

然后使用Cacheable注解的时候可以使用指定key,也可以使用Spring的自动生成策略key具体方法

@Cacheable(value = "", key =)// 自定义key策略
@Cacheable(value = "")//自动生成key策略


还有一种方式是使用SpEL表达式方式

@Cacheable(value = "example#${select.cache.timeout:1000}", key = "")//example 是cache容器名字 #后边的是SpEL表达式


不知道是否可以直接解决你的问题,如果愿意你可以作一个参考

0

请问,为什么key是用两个冒号(::)隔开得,为什么不是一个?可以更换成一个么?

撰写答案

推广链接