Reputation: 46382
The Javadoc says:
Returns a view of the entries stored in this cache as a thread-safe map. Modifications made to the map directly affect the cache.
What I'm missing is the information about whether access to the view influences the admission and eviction policies. According to this old related issue it does not:
In Guava's CacheBuilder we added the asMap() view specifically to allow bypassing the cache management routines. There a cache.asMap().get(key) is a peek operation.
This surely makes sense. OTOH the view provides many operations unavailable directly and users may be tempted to use them hoping that they update the access statistics just like direct operations.
For example, I found myself using cache.asMap().putIfAbsent
as my values are functions of the keys, so replacing them is pointless. I'd like it to work exactly as cache.put
in case the entry was absent.
Upvotes: 2
Views: 3757
Reputation: 9591
This was part of the original discussion of Guava's Map.get
, but was likely a poor idea, miscommunicated, and eventually lost. A rational was due to users not expecting side effects for most Map
operations, which MapMaker
changed with computing maps and thus broke the equals
method.
In retrospect treating any Map
methods as different breaks the principle of least astonishment and is not very useful. This was likely realized during the implementation, but due to the disjoint team and abundance of details, an aspect that I forgot. We also had decided on the principle that users shouldn't need to know how the policies work or have configuration knobs to influence their implementation, which a quiet get
would have exposed.
However, one aspect did remain for better or worse. Unlike Cache.getIfPresent
, Map.get
will not record hit rate statistics. Similarly for all other Map
operations, they may be opted out of updating CacheStats
. In Guava this is stated as,
No stats are modified by operations invoked on the asMap view of the cache.
This is slightly modified in Caffeine for additional Java 8 methods,
No stats are modified by non-computing operations invoked on the
asMap view of the cache.
Likely this opt-out of statistics should not have occurred, but is the remanent of that original discussion. This is a subtle detail that it may not be honored in full, as I believe Guava's addition of the computing Map
methods do not. Thankfully it is a minor enough detail to have not caused many issues and could be changed if deemed worthwhile.
Upvotes: 3