Skip to content

phpavan/DataGrid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot Caching with Red Hat Data Grid & PostgreSQL

This project is a Spring Boot application that integrates with Red Hat Data Grid (Remote Cache) and PostgreSQL for caching and database operations.

Features

1. Red Hat Datagrid Session Caching

2. Red Hat Datagrid API response Caching

3. Write-Behind & Read-Through Caching

4. Configurtaion based Write-Behind mecahnism is used to write image files to PoatgreDB.

Stack 1. Spring Boot with JPA & Hibernate 2. Infinispan (Red Hat Data Grid) for Caching 3. PostgreSQL as the database 4. Spring Boot REST APIs

Prerequisites Ensure you have the following installed: 1. Java 17+ 2. Maven 3+ 3. Docker (for PostgreSQL and Data Grid, if running locally) 4. Red Hat Data Grid (Remote Server)

Setup Instructions

1️⃣ Clone the Repository git clone https://github.com/phpavan/DataGrid.git cd

2️⃣ Configure Database

  Option 1: Run PostgreSQL in Docker

      docker run --name postgres-db -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=admin -e POSTGRES_DB=mydb -p 5432:5432 -d postgres

      Update application.properties to match your database config.

  Option 2: Use an Existing PostgreSQL Instance

      Modify the spring.datasource.url in application.properties:

      spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
      spring.datasource.username=admin
      spring.datasource.password=admin

3️⃣ Configure Red Hat Data Grid (Remote)

If running locally via Docker:

docker run -d -p 11222:11222 --name datagrid-server quay.io/infinispan/server:latest

Create 3 Caches in Datagrid with below specs:

    Cache : session

        {
          "sessions": {
            "distributed-cache": {
              "owners": "2",
              "mode": "SYNC",
              "statistics": true,
              "encoding": {
                "media-type": "application/x-protostream"
              }
            }
          }
        }

    Cache : mockedApiResponse

        {
          "mockedApiResponse": {
            "distributed-cache": {
              "owners": "2",
              "mode": "SYNC",
              "statistics": true,
              "encoding": {
                "media-type": "application/json"
              }
            }
          }
        }

    Cache : users-cache

        {
          "users-cache": {
            "distributed-cache": {
              "owners": "2",
              "mode": "SYNC",
              "statistics": true,
              "encoding": {
                "media-type": "application/json"
              }
            }
          }
        }
        
Ensure Infinispan/Remote Data Grid is properly configured in application.properties:

infinispan.remote.server-list=localhost:11222

4️⃣ Build & Run the Application

Build the project:

mvn clean install

Run the project:

  mvn spring-boot:run

5️⃣ Testing the APIs

Once the application is running, access the APIs via:

Swagger UI: http://localhost:8080/swagger-ui/index.html
Screenshot 2025-04-14 at 12 16 54 AM
Example Endpoints:
   1. To Test Session Caching
         curl -X POST http://localhost:8080/session/set?key=username&value=JohnDoe
          You can see Springboot session & attributes are cached in Datagrid server, In Cache named: "sessions"
         curl -X GET http://localhost:8080/session/get?key=username
          You can see Springboot session & attributes are queried from Datagrid server. Even if you restart the Springboot service, you can retrieve previous session.
          
   2. To test API Caching
        curl -X GET http://localhost:8080/fetch-data?id=123
          You can see json response from the Rest API is cached in Datagrid server, In Cache named: "mockedApiResponse"
image
   3. To test DB Caching (Read Through & WriteBehind)
        curl -X GET http://localhost:8080/users/2
        curl -X POST http://localhost:8080/users
         body - {"email": "sam@rh.com", "name": "Sam Altman", "version": 1}
image
  1. To test Image caching (Write behind mechanism enter url "http://localhost:8080/upload.html" in the browser
image
       upload any image of your choice

       you will see the image uploaded to DB via Datagrid to image_image table and also springboot uploading the image to Image table                  directly.

       However you will notice the push to DB directly took longer than writing to Datagrid.
Screenshot 2025-04-14 at 12 43 08 AM

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published