Skip to content

Application showcasing spring redis module and simple cache implementation

License

Notifications You must be signed in to change notification settings

souravkantha/spring-redis-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spring-redis-cache

Application showcasing simple write through cache implementation using spring-redis module and jedis client

@Cacheable(value = "product", key = "#productId")

@CachePut(value = "product", key = "#productId")

@CacheEvict(cacheNames="product", key="#productId")

Read more about :- https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/

image

Java Version

Used open-jdk version 17 (https://openjdk.org/projects/jdk/17/)

Redis Installation

https://redis.io/docs/getting-started/installation/

Upon successful redis installation and running standalone redis, you should be able to view below screenshot

Screenshot 2023-05-11 at 2 01 30 AM

Build Tool

We are using maven tool to build the project. You can download & install maven from official website (https://maven.apache.org/install.html)

Making the build

Download the source code using git ssh or https from https://github.com/souravkantha/spring-redis-cache

- [x] Check if maven is installed by putting 'mvn' command in command prompt or terminal

  • Go the root directory i.e; spring-redis-cache

  • Use command mvn clean package. After the build is successful, run the program using following command

    java -jar target/cache-1.0.jar

Upon successful start of the service, you should see the h2 database console

Open h2 console : http://localhost:8080/h2-console/login.jsp

Screenshot 2023-05-10 at 11 44 29 PM Screenshot 2023-05-10 at 11 56 25 PM

Test data added using data.sql (in resources directory)

INSERT INTO ITEMS VALUES (10000, 'Kelloggs Crunchy Granola Almonds and Cranberries', 'Kelloggs Crunchy Granola Almonds and Cranberries','Breakfast Cereal', 330, 1, 1);    
INSERT INTO ITEMS VALUES (10001, 'Kelloggs Crunchy Granola Chocolate & Almonds', 'Kelloggs Crunchy Granola Chocolate & Almonds', 'Breakfast Cereal', 350, 1, 1);    
INSERT INTO ITEMS VALUES (10002, 'Kelloggs muesli', 'Kelloggs muesli','Breakfast Cereal', 270, 1, 1);    
INSERT INTO ITEMS VALUES (10003, 'Kelloggs Chocos', 'Kelloggs Chocos','Breakfast Cereal', 350, 1, 1);    
INSERT INTO ITEMS VALUES (10004, 'Kelloggs Corn Flakes Real Almond Honey', 'Kelloggs Corn Flakes Real Almond Honey', 'Breakfast Cereal', 195, 1, 1); 

To test using curl

Create Product (using

curl --location --request POST 'http://localhost:8080/products' --header 'Content-Type: application/json' --data-raw '{"productId": 20000,"productName": "Kelloggs Crunchy Granola", "productDescription": "Kelloggs Crunchy Granola Almonds and Cranberries","type": "Breakfast Cereal","price": 330,"quantity": 1,"version": 1}'

Check with redis-cli after new product creation (cache is added for key product::20000)

Screenshot 2023-05-11 at 6 33 39 PM

Update Product

curl --location --request PATCH 'http://localhost:8080/products/20000' --header 'Content-Type: application/json' --data-raw '{"productId": 20000,"productName": "Kelloggs Crunchy Granola Almonds 1 Kg","productDescription": "Kelloggs Crunchy Granola Almonds and Cranberries","type":"Breakfast Cereals","price": 330,"quantity": 1,"version": 1}'

Get Product

curl --location --request GET 'http://localhost:8080/products/20000'

Delete Product

curl --location --request DELETE 'http://localhost:8080/products/20000'

Check with redis-cli after product deletion (cache is evicted for key product::20000)

Screenshot 2023-05-11 at 6 37 04 PM

About

Application showcasing spring redis module and simple cache implementation

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published