Whenever we want to reduce the amount of data fetched from database or when a low latency is the most important requirement, we think of a solution that is always the right answer: caching.
This very quick post is going to show how to integrate a cache framework in your microservice. JetCache(link here) is another cache solution that praises itself as being better than default solution provided by Spring (Spring Cache). This is the kind of debate we don’t want to enter, but we can say we love JetCache because:
- It’s very easy to integrate in a Spring Boot application
- It has support for 2 level caching: a local one and a distributed layer where we can use Redis (AWS ElastiCache) for example
As always, we implemented a very basic Spring Boot application (available here) that can be cloned and inspected.
All details about how to start it are listed in the README file. We used a Redis cluster for the distributed caching layer.
The application has 2 APIs: one for adding one element in the cache and one for retrieving. If you wait for more than 30 seconds after you adding an element, you’ll see that it’s not found since it’s automatically evicted.
curl -X PUT http://localhost:8080/put/myid/AnnaKarenina curl -X GET http://localhost:8080/get?id=myid Title: AnnaKarenina
Don’t hesitate to add a comment below!
Happy cloud computing!