有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java收回EhCache中的所有元素

我将EhCache与spring一起使用,需要公开一个端点,该端点将逐出给定EhCache中的所有元素。但我找不到任何方法来逐出所有元素。这是微不足道的,可能已经讨论过了,但我在互联网上找不到任何资源。请提供指针


共 (1) 个答案

  1. # 1 楼答案

    您正在使用Spring缓存吗?然后 将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());
    }