Skip to content
Ben Manes edited this page Mar 23, 2015 · 8 revisions

A Cache is similar to ConcurrentMap, but not quite the same. The most fundamental difference is that a ConcurrentMap persists all elements that are added to it until they are explicitly removed. A Cache on the other hand is generally configured to evict entries automatically, in order to constrain its memory footprint. In some cases a LoadingCache or AsyncLoadingCache can be useful even if it doesn't evict entries, due to its automatic cache loading.

Caffeine provide flexible construction to create a cache with any combination of the following features:

  • automatic loading of entries into the cache, optionally asynchronously
  • least-recently-used eviction when a maximum size is exceeded
  • time-based expiration of entries, measured since last access or last write
  • keys automatically wrapped in weak references
  • values automatically wrapped in weak or soft references
  • notification of evicted (or otherwise removed) entries
  • accumulation of cache access statistics
Interface Description
Caffeine Builds cache instances based on a wide range of features
Cache Synchronous cache that allows entries to be manually added
LoadingCache Synchronous cache that automatically loads entries when not present
AsyncLoadingCache Asynchronous cache that automatically loads entries when not present and returns a CompletableFuture
CacheLoader Computes or retrieves values, based on a key, for use in populating a cache
Weigher Calculates the size of entries relative to each other; used if a maximum weight is specified
RemovalListener Listens for notifications when an entry is removed from a cache
CacheStats Statistics about the performance of a cache
Policy Access to inspect and perform low-level cache operations based on its constructed configuration
CaffeinatedGuava An adapter that exposes a Caffeine cache through Guava facades
Clone this wiki locally