Caffeine is a high performance, near optimal caching library based on Java 8. For more details, see our user's guide and browse the API docs for the latest release.
Caffeine provides an in-memory cache using a Google Guava inspired API. The improvements draw on our experience designing Guava's cache and ConcurrentLinkedHashMap.
LoadingCache<Key, Graph> graphs = Caffeine.newBuilder()
.maximumSize(10_000)
.expireAfterAccess(5, TimeUnit.MINUTES)
.refreshAfterWrite(1, TimeUnit.MINUTES)
.build(key -> createExpensiveGraph(key));
Caffeine provides flexible construction to create a cache with a combination of the following features:
- automatic loading of entries into the cache, optionally asynchronously
- size-based eviction when a maximum is exceeded based on frequency and recency
- time-based expiration of entries, measured since last access or last write
- asynchronously refresh when the first stale request for an entry occurs
- keys automatically wrapped in weak references
- values automatically wrapped in weak or soft references
- notification of evicted (or otherwise removed) entries
- writes propagated to an external resource
- accumulation of cache access statistics
In addition, Caffeine offers the following extensions:
- A short look at what Caffeine brings to your applications.
- A in-depth description of Caffeine's architecture.
- Caffeine is presented as part of a research paper evaluating its novel eviction policy.
- TinyLFU: A Highly Efficient Cache Admission Policy by Gil Einziger, Roy Friedman, Ben Manes
Integrations,
- Spring Cache (upcoming Spring 4.3 & Boot 1.4 releases)
- ScalaCache: Simple caching in Scala
- Scaffeine: Scala wrapper for Caffeine
On the radar,
- Early discussions with HBase, Cassandra, Solr, and ElasticSearch
- Postgres is evaluating whether to port the cache
- Go implementation of the W-TinyLfu policy
- Ratpack is considering adoption for v1.3
Download from Maven Central or depend via Gradle:
compile 'com.github.ben-manes.caffeine:caffeine:2.2.3'
// Optional extensions
compile 'com.github.ben-manes.caffeine:guava:2.2.3'
compile 'com.github.ben-manes.caffeine:jcache:2.2.3'
See the release notes for details of the changes.
Snapshots of the development version are available in Sonatype's snapshots repository.