Reputation: 6816
Following this blog post about implementing a Caffeine async cache, we wanted to get the statistical data from the cache.
We are using version 2.7.0
of Caffeine
However, it seems that the AsyncCache
has no access to its statistics:
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
Also, when checking the source code of the AsyncCache and comparing it to the Cache class, there is no stats()
method in the async class.
Is there a reason for that?
Upvotes: 2
Views: 4565
Reputation: 9591
The AsyncCache offers a synchronous()
view to provide a Cache that blocks until the asynchronous computation completes.
/**
* 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();
This can be handy to perform operations, such as invalidate(key)
, that doesn't have an asynchronous counterpart. It also provides access to statistics and policy metadata.
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());
In this case we expect the first miss to load the entry, so that subsequent lookups are a hit.
CacheStats{hitCount=3, missCount=1, loadSuccessCount=1, loadFailureCount=0, totalLoadTime=8791091, evictionCount=0, evictionWeight=0}
Upvotes: 7