Patrick M
Patrick M

Reputation: 10989

Is there anyway to tell that a Guava Cache was constructed with stats disabled?

recordStats
public CacheBuilder<K,V> recordStats()
Enable the accumulation of CacheStats during the operation of the cache. Without this Cache.stats() will return zero for all statistics. Note that recording stats requires bookkeeping to be performed with each operation, and thus imposes a performance penalty on cache operation.

From JavaDocs for [CacheBuilder.recordStats()][2], emphasis added.

From what I can tell, there is no way to distinguish between a cache that has never been called and a cache that was constructed without recordStats() being called to enable the accumulation of stats. The Cache interface itself doesn't seem to expose any indication. Am I wrong?

If there is no such method or flag recorded, why not? 9 times out of 10, it should be pretty obvious that your cache is in fact being invoked, but I wasted an hour today trying to figure out why my cache wasn't recording stats...

Upvotes: 1

Views: 254

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198391

No, and unfortunately there's not going to be.

kevinb9n commented 2015-07-30T18:17:42Z
I remember agonizing over whether to try to change the behavior to return -1s years ago, and unfortunately we already felt it was too late. It's also not feasible to add new interface methods at this point. We are sorry.

With interfaces no changes are ever backward-compatible; it must always break either a consumer or an implementor. In this case every concrete class implementing Cache outside Guava would break. After we're on Java 8, we can add interface methods by supplying a reasonable "default" implementation. But note that in this case the best we could do is probably throw new UnsupportedOperationException(); which isn't too exciting.

Upvotes: 2

Related Questions