cache2k focuses on providing a well engineered in-memory object cache implementation for Java applications.

GitHub Stars License Stack Overflow Maven Central

  Cache<String,String> cache = new Cache2kBuilder<String, String>() {}
    .expireAfterWrite(5, TimeUnit.MINUTES)    // expire/refresh after 5 minutes
    .resilienceDuration(30, TimeUnit.SECONDS) // cope with at most 30 seconds
                                              // outage before propagating 
                                              // exceptions
    .refreshAhead(true)                       // keep fresh when expiring
    .loader(this::expensiveOperation)         // auto populating function

For a detailed introduction continue with Getting Started.

Features at a glance


  • Spring Framework
  • Scala Cache
  • Datanucleus (via JCache)
  • Hibernate (via JCache)
  • …. and probably more, please raise an issue and get it listed!



Please use the GitHub issue tracker for bugs and wishes you step upon. We also monitor the tag cache2k for general usage questions.


The code is released under Apache license.


cache2k does not try to be a full blown distributed enterprise caching solution. Some alternatives which you should take a look on:

  • Google Guava Cache
  • EHCache
  • JCS
  • Caffeine

Distributed caches:

  • Infinispan
  • hazelcast
  • redsisson
  • Apache ignite


Back to top

Version: 1.2.4.Final. Last Published: 2019-12-16.