具有统计信息的异步 Caffeine 缓存
Asynchronous Caffeine cache with statistics
关注 this 博客 post 关于实现 Caffeine 异步缓存的内容,我们想从缓存中获取统计数据。
我们使用的是 Caffeine2.7.0
版本
但是,AsyncCache
似乎无法访问其统计信息:
private AsyncCache<String, Cat> asyncCache = Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS)
.recordStats()
.maximumSize(100)
.buildAsync();
private Cache<String, Cat> cache = Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS)
.maximumSize(100)
.recordStats()
.build();
....
cache.stats(); // this is possible
asyncCache.stats(); // no such method in asyncCache
另外,在查看AsyncCache and comparing it to the Cacheclass的源码时,asyncclass中没有stats()
方法。
这有什么原因吗?
AsyncCache 提供了一个 synchronous()
视图来提供一个阻塞直到异步计算完成的缓存。
/**
* Returns a view of the entries stored in this cache as a synchronous {@link Cache}. A mapping is
* not present if the value is currently being loaded. Modifications made to the synchronous cache
* directly affect the asynchronous cache. If a modification is made to a mapping that is
* currently loading, the operation blocks until the computation completes.
*
* @return a thread-safe synchronous view of this cache
*/
Cache<K, V> synchronous();
这可以很方便地执行操作,例如 invalidate(key)
,没有异步对应项。它还提供对统计信息和政策元数据的访问。
AsyncCache<Integer, Integer> cache = Caffeine.newBuilder()
.maximumSize(10_000)
.recordStats()
.buildAsync();
// Perform application work
for (int i = 0; i < 4; i++) {
cache.get(1, key -> key);
}
// Statistics can be queried and reported on
System.out.println(cache.synchronous().stats());
在这种情况下,我们希望第一个未命中加载条目,以便后续查找成功。
CacheStats{hitCount=3, missCount=1, loadSuccessCount=1, loadFailureCount=0, totalLoadTime=8791091, evictionCount=0, evictionWeight=0}
关注 this 博客 post 关于实现 Caffeine 异步缓存的内容,我们想从缓存中获取统计数据。
我们使用的是 Caffeine2.7.0
版本
但是,AsyncCache
似乎无法访问其统计信息:
private AsyncCache<String, Cat> asyncCache = Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS)
.recordStats()
.maximumSize(100)
.buildAsync();
private Cache<String, Cat> cache = Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS)
.maximumSize(100)
.recordStats()
.build();
....
cache.stats(); // this is possible
asyncCache.stats(); // no such method in asyncCache
另外,在查看AsyncCache and comparing it to the Cacheclass的源码时,asyncclass中没有stats()
方法。
这有什么原因吗?
AsyncCache 提供了一个 synchronous()
视图来提供一个阻塞直到异步计算完成的缓存。
/**
* Returns a view of the entries stored in this cache as a synchronous {@link Cache}. A mapping is
* not present if the value is currently being loaded. Modifications made to the synchronous cache
* directly affect the asynchronous cache. If a modification is made to a mapping that is
* currently loading, the operation blocks until the computation completes.
*
* @return a thread-safe synchronous view of this cache
*/
Cache<K, V> synchronous();
这可以很方便地执行操作,例如 invalidate(key)
,没有异步对应项。它还提供对统计信息和政策元数据的访问。
AsyncCache<Integer, Integer> cache = Caffeine.newBuilder()
.maximumSize(10_000)
.recordStats()
.buildAsync();
// Perform application work
for (int i = 0; i < 4; i++) {
cache.get(1, key -> key);
}
// Statistics can be queried and reported on
System.out.println(cache.synchronous().stats());
在这种情况下,我们希望第一个未命中加载条目,以便后续查找成功。
CacheStats{hitCount=3, missCount=1, loadSuccessCount=1, loadFailureCount=0, totalLoadTime=8791091, evictionCount=0, evictionWeight=0}