Patrick M
Patrick M

Reputation: 10989

When should a Guava LoadingCache with expireAfterWrite set call cleanUp()?

This seems like it would be a simple thing to discover on my own, but here I am.

The base Guava Cache Interface only has this to say about cleanUp():

void cleanUp()
Performs any pending maintenance operations needed by the cache. Exactly which activities are performed -- if any -- is implementation-dependent.

And CacheBuilder() says this:

If expireAfterWrite or expireAfterAccess is requested entries may be evicted on each cache modification, on occasional cache accesses, or on calls to Cache.cleanUp(). Expired entries may be counted by Cache.size(), but will never be visible to read or write operations.

If weakKeys, weakValues, or softValues are requested, it is possible for a key or value present in the cache to be reclaimed by the garbage collector. Entries with reclaimed keys or values may be removed from the cache on each cache modification, on occasional cache accesses, or on calls to Cache.cleanUp(); such entries may be counted in Cache.size(), but will never be visible to read or write operations.

Certain cache configurations will result in the accrual of periodic maintenance tasks which will be performed during write operations, or during occasional read operations in the absence of writes. The Cache.cleanUp() method of the returned cache will also perform maintenance, but calling it should not be necessary with a high throughput cache. Only caches built with removalListener, expireAfterWrite, expireAfterAccess, weakKeys, weakValues, or softValues perform periodic maintenance.

So cleanUp may or may not do anything and it may or may not have any benefits to being called. But near as I can tell, the LocalCache.cleanUp() method does something, but I'm just not following what. Broadly speaking, when should a LoadingCache have cleanup() called?

Note: I linked to the LocalCache reference of cleanUp because that's what my debugger says gets instantiated when I call .load() on a CacheBuilder in Guava 18.

Specifically, my use case is a standard database backed loading cache. We've been asked to expose the ability to wipe certain caches in the event of some database records changing or being deleted and the expiration period not being fast enough. We've taken the fast and dirty approach and allowed the suspect caches to be cleared with invalidateAll(). Should we call cleanUp() afterwards? Why or why not?

Upvotes: 1

Views: 2282

Answers (0)

Related Questions