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/
Used open-jdk version 18 (https://openjdk.org/projects/jdk/18/)
https://redis.io/docs/getting-started/installation/
Upon successful redis installation and running standalone redis, you should be able to view below screenshot
We are using maven tool to build the project. You can download & install maven from official website (https://maven.apache.org/install.html)
Download the source code using git ssh or https from https://github.com/souravkantha/spring-redis-cache
-
Go the root directory i.e; spring-redis-cache
-
Use command
mvn clean package
. After the build is successful, run the program using following commandjava -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
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);
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}'
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}'
curl --location --request GET 'http://localhost:8080/products/20000'
curl --location --request DELETE 'http://localhost:8080/products/20000'
redis-cli