SpringCache 给不同的key设置不同的过期时间

前言

spring cache可以使用注解来很方便的操作缓存,但是它的过期时间配置却是统一的,如何自定义每个key的过期时间?

关键点

以springboot+redis为例,使用注解来操作redis之前需要有一个redis配置类

@Configuration
//@EnableConfigurationProperties({Properties.class})
@EnableCaching
public class RedisConfig {

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        
    }

}

对redis的操作都交给了这个CacheManager的Bean来完成
包括对key的管理,超时时间的设置等都可以在这里进行配置

实现

  • 在配置文件中定义key的过期时间
server.port=8080
spring.redis.database=2
spring.redis.host=127.0.0.1
#spring.redis.password=123456
spring.redis.port=6379

initCaches.key1 = 10s
initCaches.key2 = 20s
  • 定义一个配置类映射配置文件的key
@Component
@ConfigurationProperties
public class Properties {

    private final Map initCaches = new HashMap<>();

    public Map getInitCaches() {
        return initCaches;
    }
}
  • redis配置类
@Configuration
//@EnableConfigurationProperties({Properties.class})
@EnableCaching
public class RedisConfig {

    @Autowired
    private Properties properties;

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(10)).disableCachingNullValues();

        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);

        Set cacheNames =new HashSet<>();
        ConcurrentHashMapcacheConfig = new ConcurrentHashMap<>();
        for (Map.Entry entry : properties.getInitCaches().entrySet()) {
            cacheNames.add(entry.getKey());
            cacheConfig.put(entry.getKey(), defaultCacheConfig.entryTtl(entry.getValue()));
        }

        return RedisCacheManager.builder(redisCacheWriter)
                .cacheDefaults(defaultCacheConfig)
                .initialCacheNames(cacheNames)
                .withInitialCacheConfigurations(cacheConfig)
                .build();
    }

}

其实核心属性就是两个集合:cacheNames和cacheconfigurations

  • 测试
  @GetMapping("/put1")
    @Cacheable(value = "key1", key = "#id")
    public String putKey1(String id) {
        return "key1Value";
    }
    @GetMapping("/put2")
    @Cacheable(value = "key2", key = "#id")
    public String putKey2(String id) {
        return "key2Value";
    }

value就是配置文件定义的key,key是支持#spel表达式的,方法必须带有参数。

测试结果key1过期时间为10s,key2过期时间为20s

你可能感兴趣的:(springboot,Java后台,redis)