Caffeine Cache: Algorithm Advantages, Usage Strategies, and Spring Boot Integration
This article explains the high‑performance Caffeine Cache library, its W‑TinyLFU eviction algorithm, various loading and eviction strategies, statistics collection, and how to integrate and configure it in Spring Boot using annotations and programmatic beans.
Introduces Caffeine Cache, a high‑performance local cache library that improves upon Guava Cache by using the modern W‑TinyLFU eviction algorithm.
Algorithm advantages – W‑TinyLFU
Explains FIFO, LRU, LFU limitations and how W‑TinyLFU combines LFU’s high hit rate with LRU’s handling of burst traffic, using Count‑Min Sketch and a sliding‑window reset to keep frequency information lightweight.
Usage
Shows three loading strategies: manual loading with cache.get(key, k->...) , synchronous loading via LoadingCache with a CacheLoader , and asynchronous loading with AsyncLoadingCache and CompletableFuture .
Eviction policies
Describes size‑based, time‑based and reference‑based eviction, including .maximumSize() , .expireAfterWrite() , .expireAfterAccess() , .weakKeys() , .softValues() , and the incompatibilities between some options.
Removal listener and CacheWriter
Provides examples of .removalListener((key, value, cause) -> System.out.printf("Key %s was removed (%s)%n", key, cause)) and .writer(new CacheWriter () { public void write(String key, Object value) { /* write to external store */ } public void delete(String key, Object value, RemovalCause cause) { /* delete from external store */ } }) for custom actions on eviction or persistence.
Statistics
Enables .recordStats() and shows how to retrieve hit rate, eviction count, and average load penalty from CacheStats .
Spring Boot integration
Explains that Spring Boot 2.x replaces Guava with Caffeine as the default local cache, shows Maven dependencies, the @EnableCaching annotation, configuration via application.properties or YAML, and a CacheLoader bean for refreshAfterWrite .
Cache annotations
Details @Cacheable , @CachePut , @CacheEvict , @Caching , and @CacheConfig , including SpEL key expressions, condition/unless clauses, and the sync option for synchronized cache population.
Provides a complete example service class using these annotations and a programmatic SimpleCacheManager with multiple CaffeineCache beans:
public class UserCacheService {
@Cacheable(value = "userCache", key = "#id", sync = true)
public void getUser(long id) { /* query DB */ }
@CachePut(value = "userCache", key = "#user.id")
public void saveUser(User user) { /* save DB */ }
@CacheEvict(value = "userCache", key = "#user.id")
public void delUser(User user) { /* delete DB */ }
}Also shows how to create a SimpleCacheManager bean that registers several CaffeineCache instances with custom TTL and maximum size.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.