set expire key at specific time when using Spring caching with Redis

萝らか妹 提交于 2019-12-12 12:18:28

问题


I am spring caching abstraction annotations to apply caching to my service methods.

Since I am using Redis as the cache store, I want to use the option of expiring cache at a specific time, since that is supported by Redis. expireat command in redis can be used to set expire time at a future time.

I am not sure how I can do that for the keys which are part of my cache when using RedisCache.

I tried to customize RedisCacheManager by creating a bean of it.

I see there is a getNativeCache() method exposed. but I did not find any way to set value for expireat using it.

If there is a way to customize RedisCacheManager so that all keys of a specifc cache use the same time as expiry, please let me know.


回答1:


@Bean (name="cacheManager")
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
    RedisCacheConfiguration conf_ready_info = RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofMillis(50000));

    RedisCacheConfiguration conf_base_info = RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofMillis(60000));

    Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<String, RedisCacheConfiguration>();
    cacheConfigurations.put("base_info", conf_base_info);
    cacheConfigurations.put("ready_info", conf_ready_info);

    return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(connectionFactory)
            .withInitialCacheConfigurations(cacheConfigurations).build();
}


@Cacheable(value = "ready_info", key = "#aid")
public String  findByAid(String aid) throws Exception { 
    String readyInfo = "";
    return readyInfo;
}



回答2:


Actually there is. Please check below:

RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig()
            .disableCachingNullValues()
            .entryTtl(<your duration>)
            .computePrefixWith(CacheKeyPrefix.simple())
            .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory)
            .cacheDefaults(configuration)
            .build();

I am using 2.0.4.RELEASE version of spring boot. And if you are using older versions here is how you can do:

RedisCacheManager cacheManager =
            new RedisCacheManager(
                    redisTemplate,//redis template
                    Arrays.asList("my-cahce"),//loads these caches at startup
                    false);//Do not allow null values
    cacheManager.setLoadRemoteCachesOnStartup(true);
    cacheManager.setCachePrefix(new DefaultRedisCachePrefix());
    cacheManager.setDefaultExpiration(TimeUnit.SECONDS.convert(365, TimeUnit.DAYS));


来源:https://stackoverflow.com/questions/51660160/set-expire-key-at-specific-time-when-using-spring-caching-with-redis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!