Skip to content

Removal

Ben Manes edited this page Mar 23, 2015 · 13 revisions

Terminology:

  • eviction means removal due to the policy
  • invalidation means manual removal by the caller
  • removal occurs as a consequence of "invalidation" and "evicted"

Explicit Removals

At any time, you may explicitly invalidate cache entries rather than waiting for entries to be evicted.

// individual key
cache.invalidate(key)
// bulk keys
cache.invalidateAll(keys)
// all keys
cache.invalidateAll()

Removal Listeners

Cache<Key, Graph> graphs = Caffeine.newBuilder()
    .removalListener(notification ->
      System.out.println("Removed: " + notification))
    .build();

You may specify a removal listener for your cache to perform some operation when an entry is removed, via Caffeine.removalListener(RemovalListener). The RemovalListener gets passed a RemovalNotification which specifies the RemovalCause, key, and value.

Removal listener operations are executed asynchronously using an Executor. The default executor is ForkJoinPool.commonPool() and can be overridden via Caffeine.executor(Executor).

Note that any exceptions thrown by the RemovalListener are logged (using Logger) and swallowed.

Clone this wiki locally