Search code examples
springspring-cache

CaffeineCacheManager vs SimpleCacheManager


May I know different between CaffeineCacheManager and SimpleCacheManager?

As description CaffeineCacheManager is Lazy cache, but what is lazy cache, in what situation I should pick CaffeineCacheManager?


Solution

  • Have a read of all the different cache providers first and notice how their APIs differ. The Simple Cache manager is Spring's default cache manager that's used if you don't specify a cache manager. It's 'simple' because its underlying implementation uses a Java ConcurrentHashMap and it doesn't give you a whole lot of customization options.

    The Caffeine Cache manager is slightly different in that there a more configuration-driven customization options such as having the ability to specify the cache timeout expiry limit (in order to 'bust' the cache after a certain time period) and the cache maximum size limit in order to limit the capacity of the cache. The default cache manager does not give you this configurability.

    My team used the Caffeine Cache manager on a project recently and I can definitely recommend it.

    In terms of your question about a 'lazy' cache. Have a read of lazy instatiation more broadly. Essentially it only loads what it needs when it needs it (upon a cache access) rather than loading the entire thing all at once.