驱逐EhCache中的所有元素

问题描述 投票:0回答:1

我正在使用带有弹簧的EhCache并且需要暴露一个端点,该端点将驱逐给定EhCache中的所有元素。但我无法找到任何驱逐所有元素的方法。这是微不足道的,可能已经讨论过但我在互联网上找不到任何资源。请提供指示。

java spring ehcache spring-cache
1个回答
2
投票

你在使用Spring Cache吗?然后将true设置为allEntries属性

@Cacheable("myCache")
public String getCache() {
    try {
        Thread.sleep(3000);
    } catch (final InterruptedException e) {
    }
    return "aaa";
}

@CacheEvict(cacheNames = "myCache", allEntries = true)
public void evictAll() {
}

或者如果要删除您定义的所有缓存

@Autowired
CacheManager cacheManager;

public void evictAll() {
    cacheManager.getCacheNames()
            .stream()
            .forEach(n -> cacheManager.getCache(n).clear());
}
© www.soinside.com 2019 - 2024. All rights reserved.