Search code examples
javaspringspring-bootcachingspring-cache

What are main benefits/differences [ spring-cache vs plain map caching ]


So I recently added some spring-cache usage into my services bud the more i look at the the more i feel it just limits what i can do.

My initial impressions is that its less code heavy, i dont have to write as much code. Thats a plus.

Negative is that I dont have control over what gets cached as much, Usage of Ids, vs collections of entities passed to methods like saveAll etc, Also some methods that execute more complicated queries.

Is there something underneeth that im missing when it comes to spring cache? Some magic in the background or is this really just as simple as creating some concurrent hashmaps and putting data to them from services and taking data out or updating based on service method invocations?

Im 50 / 50 on using my own caching implementation vs spring-cachce. What are risks that i can run into that spring-cache already solves for me?

This question is under assumption that default cache manager is used.


Solution

  • Spring uses a default ConcurrentHashMap by default but if you include Caffeine (https://github.com/ben-manes/caffeine) or Hazelcast in the classpath you get a different cache provider. The beauty of this is that you can configure the cache outside or your application without any code.