Home Post Spring Framework

Spring Data Redis with RedisTemplate and CrudRepository

Jun 18, 2023 | Updated Jun 19, 2023

In this article, we will see how to use Spring Data Redis to communicate (using RedisTemplate and CrudRepository) with a Redis instance or cluster.

1) Run Redis in a Docker Container

To run Redis in a Docker container, you can follow these steps:

1) Make sure you have Docker installed on your machine. You can download and install Docker from the official Docker website.

2) Open a terminal or command prompt and run the following command to pull the Redis Docker image from Docker Hub:

docker pull redis

3) Once the Redis image is pulled, you can start a Redis container using the following command:

docker run --name my-redis-container -d -p 6379:6379 redis

4) To verify that the Redis container is running, you can use the following command:

docker ps

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                    NAMES
c6987af34bba   redis     "docker-entrypoint.s…"   29 seconds ago   Up 28 seconds   0.0.0.0:6379->6379/tcp   my-redis-container

5) Run the following command to start a new redis-cli session and connect to the Redis container:

docker exec -it c6987af34bba redis-cli

2) Set up a Spring Boot project

Create a new Spring Boot project or use an existing one. You can set up a new project using Spring Initializr or your preferred IDE.

2.1) Dependencies

In your project's pom.xml, add the following dependencies:

2.2) Properties

In your application.properties or application.yml file, configure the Redis connection properties.

2.3) Redis configuration

Create a new RedisConfig.java class to configure Redis. This configuration sets up a "Redis connection factory" and a "Redis template bean", which allows you to interact with Redis.

2.4) Using the RedisTemplate

In this, the RedisTemplateService injects the RedisTemplate bean and provides methods to set and get values from Redis.

2.4.1) Create a REST Controller

Create a "RedisController" class and annotate it with @RestController. This class will handle the HTTP requests and define the API endpoints.

2.5) Using repositories

2.5.1) Create a Redis entity

Define a Java class that represents your Redis entity. This class should have the necessary annotations to specify the Redis key and any additional properties.

The User class is annotated with @RedisHash("users") to indicate that it maps to the "users" Redis hash.

2.5.2) Create a repository interface

The UserRepository interface provides basic CRUD operations (create, read, update, delete) for the User entity. You can also define additional custom query methods if needed.

2.5.2) Perform Redis operations

The UserServiceImpl class injects the UserRepository and uses it to save and retrieve User entities.

2.5.3) Create a REST Controller

Create a "UserController" class and annotate it with @RestController. This class will handle the HTTP requests and define the API endpoints.

2.6) Run the application

Run the Spring Boot application using your preferred method (e.g., IDE, Maven).

2.7) Test

You can now test your API using a tool like cURL, Postman, or /swagger-ui. For example, you can use swagger-ui to make requests from the browser:

Swagger UI

Source Code

avatar

NK Chauhan

NK Chauhan is a Principal Software Engineer with one of the biggest E Commerce company in the World.

Chauhan has around 12 Yrs of experience with a focus on JVM based technologies and Big Data.

His hobbies include playing Cricket, Video Games and hanging with friends.

Categories
Spring Framework
Microservices
BigData
Core Java
Java Concurrency