Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可缓存弹簧靴的到期时间_Java_Caching_Spring Cache - Fatal编程技术网

Java 可缓存弹簧靴的到期时间

Java 可缓存弹簧靴的到期时间,java,caching,spring-cache,Java,Caching,Spring Cache,我已经实现了一个缓存,现在我想添加一个到期时间 如何使用@Cacheable在spring boot中设置到期时间 这是一段代码片段: @Cacheable(value="forecast",unless="#result == null") 请注意,这个答案使用了,它是受支持的Spring引导缓存管理器之一,可以说是最流行的管理器之一 首先,您需要添加到pom.xml: <!-- Spring Framework Caching Support --> <dependenc

我已经实现了一个缓存,现在我想添加一个到期时间

如何使用
@Cacheable
在spring boot中设置到期时间

这是一段代码片段:

@Cacheable(value="forecast",unless="#result == null")

请注意,这个答案使用了,它是受支持的Spring引导缓存管理器之一,可以说是最流行的管理器之一

首先,您需要添加到
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>
<?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>

直接通过缓存提供程序。缓存抽象是一种抽象,而不是缓存实现。您正在使用的解决方案可能支持其他解决方案不支持的各种数据策略和不同拓扑(例如JDK ConcurrentHashMap)——在缓存抽象中公开这些策略和拓扑将毫无用处,因为没有支持。在配置备份缓存或通过其本机API时,应直接通过备份缓存控制此类功能


不能使用@cacheable符号指定到期时间,因为@cacheable不提供任何此类可配置选项

但是,提供spring缓存的不同缓存供应商通过自己的配置提供了此功能。例如/允许您


如果您已经实现了自己的缓存,则需要在缓存提供程序中定义一种指定过期的方法,并且还需要在解决方案中包含过期逻辑。

我使用的life hacking如下所示

    @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()));
        }
    }

我使用咖啡因缓存,此配置的有效期为60分钟:

spring.cache.cache-names=forecast
spring.cache.caffeine.spec=expireAfterWrite=60m

您可以通过使用注释来实现这种逐出策略。可以使用fixedRate甚至cron表达式进行调度

@Autowired
CacheManager cacheManager;

public void evictAllCaches() {
        cacheManager.getCacheNames().stream()
          .forEach(cacheName -> cacheManager.getCache(cacheName).clear());
}

@Scheduled(fixedRate = 6000)
public void evictAllcachesAtIntervals() {
        evictAllCaches();
}

如果您正在使用咖啡因,则可以在
应用程序.properties
文件中添加以下行:

spring.cache.caffeine.spec=expireAfterAccess=300s

Spring boot没有*xmlOh是的,有!:)这是EHCache配置的默认位置。其他缓存库可能需要不同的位置。谢谢。最后,我在我的应用程序中添加了这个。yml:spring.resources.cache-period抱歉,我没有看到与您的示例相关的链接。听起来和我完全无关。该属性用于配置web资源的缓存周期。很好,但这将逐出所有条目,旧条目或新条目。