Spring Boot Cache中的Caffeine Cache:获取所有缓存的密钥

我正在将caffeine Cache库用于Spring Cache。有没有办法获取所有缓存的密钥?

我当前的应用程序处理的是近实时数据,流为:

Spring Boot Cache中的Caffeine Cache:获取所有缓存的密钥

Cache Updater Thread(无论用户请求如何,它以固定的间隔运行)中,我需要获取当前在缓存中的所有键,从Db中获取其最新数据,然后使用{{1} }以更新缓存。

pllpll2003 回答:Spring Boot Cache中的Caffeine Cache:获取所有缓存的密钥

Yo可以注入CacheManager并从中获取本机缓存。

shell

当然,您应该添加一些类转换检查。

,

您可以使用 asMap().keySet() 方法返回密钥集,如下所示。

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

class Test{
private Cache<String,String> testCache;
Test(){
 testCache = Caffeine.newBuilder().expireAfterWrite( 3000,TimeUnit.SECONDS).build();
}
     
// return keys as a set
public Set<String> getCacheKeySet(){
return testCache.asMap().keySet();
}
本文链接:https://www.f2er.com/2701165.html

大家都在问