The goal of this project is to explore how caching works. For it, we are going to implement a simple Spring Boot application called comic-api. We are using PostgreSQL for storage and, for caching, we can pick one of the following providers: Simple or Redis. Additionally, I’ve written a Medium Article related to this project.
The application uses 2 caches: COMIC and COMICS.
Caching is applied at service level. For instance, if you call GET /api/comic?name=batman for the first time, the application will check whether the comic with the name batman is present in the COMIC cache; if not, it must go to DB to get the information about the comic (payload). Let’s say that the payload is:
{ "name": "batman", "datePublished": "2020-10-10" }Before the endpoint finishes and returns the result, the key and its payload are saved in COMIC cache:
{ "batman" = {"name": "batman", "datePublished": "2020-10-10" }On subsequent calls to GET /api/comic?name=batman (and as far as the data is not evicted), the application just needs to go to the cache and get the value.
Additionally, caching is implemented when retrieving all comics. Whenever a comic is added or updated, the COMICS cache is evicted and repopulated on the next request to fetch all comics.
/api/comic/all
Open a terminal and, inside the springboot-caching-postgresql root folder, run:
docker compose up -d-
In a terminal, make sure you are inside the
springboot-caching-postgresqlroot folder; -
Start the application by picking one of the following cache providers:
-
Simplecache provider./mvnw clean spring-boot:run -
Rediscache provider./mvnw clean spring-boot:run -Dspring-boot.run.profiles=redis
-
| Application | URL |
|---|---|
comic-api |
Open a terminal and run the following commands:
-
Create a comic
curl --request POST \ --url http://localhost:8080/api/comic \ --header 'content-type: application/json' \ --data '{ "name": "batman", "datePublished": "2020-10-10" }' -
Update a comic
curl --request PUT \ --url http://localhost:8080/api/comic \ --header 'content-type: application/json' \ --data '{ "name": "batman", "datePublished": "2020-12-21" }' -
Get a comic
curl --request GET \ --url 'http://localhost:8080/api/comic?name=batman' \ --header 'content-type: application/json' -
Get all comics
curl --request GET \ --url http://localhost:8080/api/comic/all \ --header 'content-type: application/json' -
Check Simple Cache
curl --request GET \ --url 'http://localhost:8080/api/comic/cache?name=Comic' \ --header 'content-type: application/json'
-
redis-commander
redis-commanderUI can be accessed at http://localhost:8081.


