How To Implement Redis Cache In Spring Boot Application with mysql database

How To Implement Redis Cache In Spring Boot Application with mysql database

In this post we will show how to use Redis caching in a simple spring boot application using with ysql database .we will try to focus on a simple problem of providing caching to a crud sample rest service that provides employee related data.This data is in a database, but caching will help us improve the performance instead of hitting the databse multiple times.
SpringBoot with redis cache and mysql Database

Before dive into topic What is Redis? 

It is basically a data-structure ,in-memory remote data structure store(database) that offers high performance, replication store that stores the data in the primary memory of a computer system. It uses key-value pairs to store the data, which is just like a HashMap in Java, a dictionary in Python, or an object in JavaScript. This is also why it is sometimes referred to as a NoSQL database.

What is Redis Used for?

1) In-Memory Database : Database that keeps the whole dataset in RAM
2) Cache : Increase our application performance.
3) Message Broker(MQ) : Another use of Redis is as a Message Broker.

In real time application, Redis is popular for a Cache Manager as compared to database. As a cache manager, It reduces network calls and improves the performance of an application.

What is Redis Cache?

Redis Cache is only a Cache Management highlight presented by Redis. Redis is regularly utilized as a reserve to store over and again got to information in memory so the client can feel the better presentation of the application. The Redis Cache offers varous highlights like how long you need to keep information, and which information to eliminate first, and some other splendid reserving models.

Prerequisites for springboot with azure redis cache and mysql databse
  • Java 8
  • MySQL Database Server
  • IDE which ever your comfortable
  • Maven
  • Redis Server
  • Redis Clie (Connect with server)
Required Maven Dependencies

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>

Application.properties

#Common Configuration
spring.main.banner-mode=off 
server.port=9090
logging.pattern.console=%clr(%d{yy-MM-dd E HH:mm:ss.SSS}){blue} %clr(%-5p) %clr(%logger{0}){blue} %clr(%m){faint}%n
#######################################################################
#Redius Configuration
spring.redis.host=Hostname
spring.redis.port=6379
spring.redis.password=AccessCode
#spring.redis.port=6380
#spring.redis.ssl=true
#spring.redis.timeout=1800000
spring.cache.type=redis
spring.cache.redis.cache-null-values=true
#######################################################################
#Datasource Configuration
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://hostname:3306/redis?useSSL=true&requireSSL=false&serverTimezone=GMT
spring.datasource.username=admin
spring.datasource.password=Password

#Hibernate JPA Configuration
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
#######################################################################
spring.cache.type=redis – Using this properties we are specifying cache providers. By default, auto-detected according to the environment.

important annotations to enable Redis Cache in the Application

@EnableCaching – This annotation use at the main class (starter class) of our application in order to tell Spring Container that we need Caching feature in our application.
@CachePut – This annotation is use to update the cache.
@Cacheable – This annotation indicates that the result of calling a method can be cached.
@CacheEvict – This annotation is use to remove the data from cache.

How to implement Redis Cache in Spring Boot Application?

To implenet Redis Cache utilizing Spring Boot, we want to make one little application that will have CRUD tasks set up. Then we will apply Redis Cache feature in Retrieve, Update and Delete Operations. Notwithstanding, we shouldn't matter Cache feature in a Create activity as we won't get any advantage of it. Let’s start working on ‘How to implement Redis Cache in Spring Boot?’ step by step as below:

Step1: Create a new Spring Boot Starter Project using STS or Spring initializr
Step2: Update application.properties
Step3: Add annotation @EnableCaching at starter class
Step4: Create an Entity class
Step5: Create a Repository Interface
Step6: Create Service Interface & Impl class
Step7: Create RestController class

Don't worry we will provide Git-Hub source code link for better understanding and easy to use.

How to test the application after implementing Redis Cache?

1) Start your Spring Boot Application.
2) Make a Rest Call using any REST client on operations which are get, update & delete.
3) When you call an operation for the first time, you should see a Hibernate DB query in the console.
4) If you call the same operation for the second time, you should not see the Hibernate DB query in the console. If it is, you have successfully applied the Redis Cache.

You can download the entire springboot with redis cache and mysql dabase example over Here
How to connect to redis server using the redis cli in  windows machine Check Here
Frequently using redis cache command line commands Check Here

SUBSCRIBE TO OUR NEWSLETTER

I’m the Founder of quickdevops.com. I am a Professional Blogger, Application developer, YouTuber. I’ve been blogging since 2015.I spend a lot of time learning new techniques and actively help other people learn web development through a variety of help groups and writing web development tutorials for my website and blog about advancements in web design and development.Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie or planning a trip to someplace I've never been before.

0 Response to "How To Implement Redis Cache In Spring Boot Application with mysql database"

Post a Comment