rio*_*rio 2 java guava caffeine
在这篇关于实现 Caffeine 异步缓存的博客文章之后,我们希望从缓存中获取统计数据。
我们正在使用2.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
Run Code Online (Sandbox Code Playgroud)
另外,检查AsyncCache的源代码并将其与Cachestats()类进行比较时,发现async类中没有方法。
这是有原因的吗?
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();
Run Code Online (Sandbox Code Playgroud)
这可以方便地执行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());
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我们期望第一次未命中加载该条目,以便后续查找成功。
CacheStats{hitCount=3, missCount=1, loadSuccessCount=1, loadFailureCount=0, totalLoadTime=8791091, evictionCount=0, evictionWeight=0}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3811 次 |
| 最近记录: |