Skip to content

Repository files navigation

springboot-caching-postgresql

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.

Project Diagram

diagram

Applications

comics-api Spring Boot Web Java application that has endpoints to manage comics. Data is stored in PostgreSQL and caching handled by Simple or Redis.

+ comic api swagger

Caching

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

Prerequisites

Start Environment

Open a terminal and, inside the springboot-caching-postgresql root folder, run:

docker compose up -d

Running application using Maven

  • In a terminal, make sure you are inside the springboot-caching-postgresql root folder;

  • Start the application by picking one of the following cache providers:

    • Simple cache provider

      ./mvnw clean spring-boot:run
    • Redis cache provider

      ./mvnw clean spring-boot:run  -Dspring-boot.run.profiles=redis

Application URL

Application URL

comic-api

http://localhost:8080/swagger-ui.html

Simulation

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'

About

The goal of this project is to explore how caching works. For it, we are going to implement a simple Spring Boot application called comics-api. We will use Spring Data JPA, PostgreSQL and Redis

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages