Java 过期时间@cacheable spring boot
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27968157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expiry time @cacheable spring boot
提问by nole
I have implemented a cache and now I want to add an expiry time.
我已经实现了一个缓存,现在我想添加一个到期时间。
How can I set an expiry time in spring boot with @Cacheable
?
如何在 Spring Boot 中设置到期时间@Cacheable
?
This is a code snippet:
这是一个代码片段:
@Cacheable(value="forecast",unless="#result == null")
回答by Steve
Note that this answer uses ehcache, which is one of supported Spring Boot cache managers, and arguably one of the most popular.
请注意,此答案使用ehcache,它是受支持的 Spring Boot 缓存管理器之一,可以说是最受欢迎的管理器之一。
First you need to add to pom.xml
:
首先,您需要添加到pom.xml
:
<!-- Spring Framework Caching Support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
In src/main/resources/ehcache.xml
:
在src/main/resources/ehcache.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" />
<cache name="forecast"
maxElementsInMemory="1000"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
回答by Stephane Nicoll
From the reference documentation
从参考文档
Directly through your cache provider. The cache abstraction is… well, an abstraction not a cache implementation. The solution you are using might support various data policies and different topologies which other solutions do not (take for example the JDK ConcurrentHashMap) - exposing that in the cache abstraction would be useless simply because there would no backing support. Such functionality should be controlled directly through the backing cache, when configuring it or through its native API.
直接通过您的缓存提供程序。缓存抽象是……好吧,抽象而不是缓存实现。您正在使用的解决方案可能支持其他解决方案不支持的各种数据策略和不同的拓扑(例如 JDK ConcurrentHashMap) - 在缓存抽象中公开它是无用的,因为没有后备支持。此类功能应在配置时或通过其本机 API 直接通过后备缓存进行控制。
回答by Sameer Shah
You cannot specify expiry time with @cacheable notation, since @cacheable does not provide any such configurable option.
您不能使用 @cacheable 表示法指定到期时间,因为 @cacheable 不提供任何此类可配置选项。
However different caching vendors providing spring caching have provided this feature through their own configurations. For example NCache/ TayzGridallows you to create different cache regions with configurable expiration time.
然而,提供弹簧缓存的不同缓存供应商已经通过他们自己的配置提供了这个功能。例如,NCache/ TayzGrid允许您创建具有可配置到期时间的不同缓存区域。
If you have implemented your own cache, you will need to define a way to specify expiration in you cache provider and will also need to incorporate expiration logic in your solution.
如果您实现了自己的缓存,则需要定义一种在缓存提供程序中指定到期时间的方法,并且还需要将到期逻辑合并到您的解决方案中。
回答by Atum
I use life hacking like this
我像这样使用生活黑客
@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()));
}
}
回答by slartidan
I use caffeine-caching, with this configuration for a 60 minute expiration:
我使用咖啡因缓存,此配置的有效期为 60 分钟:
spring.cache.cache-names=forecast
spring.cache.caffeine.spec=expireAfterWrite=60m