SpringBoot 2.X @Cacheable, how does redis-cache set cache time based on key?

  java, question

Since SpringBoot 2.x, the configuration of @Cacheable, Redis-cahce has changed a lot.

I found some information on the Internet. My current configuration is

@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;
 bracket

After using the above code, the cache can be successfully performed, but the cache time cannot be specified for key.

clipboard.png

As shown in the figure, two keysUserInfoListAnd keyUserInfoListAnotherThe default is 30 minutes

2.X can no longer be usedRedisCacheManager rcm = new RedisCacheManager(redisTemplate)How to configure the method to set cache time?

Google found a satisfactory method after a night’s work. Here is the entire RedisCacheConfig file.

@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());
 bracket
 stringBuilder.append("]");
 
 return stringBuilder.toString();
 };
 bracket
 
 @Bean
 public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
 return new RedisCacheManager(
 RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
 GetRediscoacheConfigurationWithTTL (600),//Default policy, unconfigured key will use this
 GetRediscoacheConfigurationMap ()//Specify key Policy
 );
 bracket
 
 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;
 bracket
 
 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;
 bracket
 bracket

To specify the expiration time of the key, simply click on thegetRedisCacheConfigurationMapMethod can be added.
Then all it takes is@CacheableYou can save the data in redis.

@ cacheable (value = "userinfolist", keygenerator = "simplekeygenerator")//3000 seconds
 @ cacheable (value = "userinfolistanonther", keygenerator = "simplekeygenerator")//18000 seconds
 @ cacheable (value = "defaultkeytest", keygenerator = "simple keygenerator")//600 seconds, unspecified key, use default policy