我可以为@Cacheable设置TTL吗?
我正在尝试对Spring 3.1的注释支持,并且想知道是否有任何方法可以通过设置TTL在一段时间后清除缓存的数据?现在从我所看到的,我需要通过使用自己清除它,并且通过使用它,我可以自己制作一个TTL实现,但对于这样一个简单的任务来说,这似乎有点多?@Cacheable
@CacheEvict
@Scheduled
我正在尝试对Spring 3.1的注释支持,并且想知道是否有任何方法可以通过设置TTL在一段时间后清除缓存的数据?现在从我所看到的,我需要通过使用自己清除它,并且通过使用它,我可以自己制作一个TTL实现,但对于这样一个简单的任务来说,这似乎有点多?@Cacheable
@CacheEvict
@Scheduled
春季3.1和番石榴1.13.1:
@EnableCaching
@Configuration
public class CacheConfiguration implements CachingConfigurer {
@Override
public CacheManager cacheManager() {
ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager() {
@Override
protected Cache createConcurrentMapCache(final String name) {
return new ConcurrentMapCache(name,
CacheBuilder.newBuilder().expireAfterWrite(30, TimeUnit.MINUTES).maximumSize(100).build().asMap(), false);
}
};
return cacheManager;
}
@Override
public KeyGenerator keyGenerator() {
return new DefaultKeyGenerator();
}
}
我像这样使用生活黑客
@Configuration
@EnableCaching
@EnableScheduling
public class CachingConfig {
public static final String GAMES = "GAMES";
@Bean
public CacheManager cacheManager() {
ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager(GAMES);
return cacheManager;
}
@CacheEvict(allEntries = true, value = {GAMES})
@Scheduled(fixedDelay = 10 * 60 * 1000 , initialDelay = 500)
public void reportCacheEvict() {
System.out.println("Flush Cache " + dateFormat.format(new Date()));
}