Prerequisites:
Knowledge in Java, Spring boot, redisclient and server.
Concept:
The purpose of this blog is to present an idea for connecting, constructing to insert and querying from Redis database using spring boot application.
What is Redis ?
Redis stands for REmote DIctionary Server. It is an in-memory key-value database server. In-memory means that redis using memory to store data instead of disks, which makes redis super fast for reading and writing data. Since redis is a key-value database server, it is a no-SQL server. It supports data structures such as strings, lists, sets, sorted sets, hashes,etc.
Technologies used:
1. Redis version 3.2.100
2. Spring boot 2.7.2
3. Java 1.8
4. Maven
Tools used:
1. Postman
2. Redis Client
3. RedisInsight
Note: The blog focuses only on a part of the CRUD operation. Click here for the complete source code with CRUD operations.
Project Structure:
Step 1: Create a Spring boot application using Spring Initalizr and select the dependencies as shown in the snapshot below.
Step 2: Add the additional dependencies given in the pom.xml file below.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.11</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.poc.redis</groupId> <artifactId>redis-springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>redis-springboot</name> <description>Demo project for integrating redis with springboot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.7.10</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
Step 3: Configuring Redis in Spring boot application.
application.yml
spring: redis: host: localhost port: 6379 cache: type: redis
AppConfig.java
package com.poc.redis.redisspringboot.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; import com.poc.redis.redisspringboot.dto.EmployeeInfo; @Configuration public class AppConfig { @Bean public RedisConnectionFactory redisConnectionFactory() { return new LettuceConnectionFactory(); } @Bean public RedisTemplate<String, EmployeeInfo> redisTemplate(){ RedisTemplate<String, EmployeeInfo> empTemplate = new RedisTemplate<>(); empTemplate.setConnectionFactory(redisConnectionFactory()); empTemplate.setKeySerializer(new StringRedisSerializer()); return empTemplate; } }
Step 4: Create Rest controller empRestController.java
@Autowired public EmployeeDetailService employeeDetailService; @PostMapping("/saveEmp") public EmployeeInfo saveEmpDetail(@RequestBody EmployeeInfo emp) { return employeeDetailService.saveEmployee(emp); } @GetMapping("/getEmp/{id}") public EmployeeInfo getEmployeeInfo(@PathVariable Integer id) throws Exception { return employeeDetailService.getEmployeeInfo(id); } @GetMapping("/allEmp") public Map<Integer, EmployeeInfo> getAllEmployeesInfo(){ return(employeeDetailService.getAllEmployeesInfo()); }
Step 5: Create a model Employee.java.
package com.poc.redis.redisspringboot.dto; import java.io.Serializable; ; public class EmployeeInfo implements Serializable { private static final long serialVersionUID = -7999224776021728682L; Integer id; String name; String department; String projectDuration; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getProjectDuration() { return projectDuration; } public void setProjectDuration(String projectDuration) { this.projectDuration = projectDuration; } public static long getSerialversionuid() { return serialVersionUID; } }
Step 6: Create an interface EmpService.java and EmpServiceImpl.java.
package com.poc.redis.redisspringboot.service; import java.util.Map; import javax.annotation.Resource; import org.springframework.data.redis.core.HashOperations; import org.springframework.stereotype.Repository; import com.poc.redis.redisspringboot.dto.EmployeeInfo; @Repository public class EmployeeDetailServiceImpl implements EmployeeDetailService { private final String hashReference= "EmployeeInfo"; @Resource(name="redisTemplate") private HashOperations<String, Integer, EmployeeInfo> hashOperations; @Override public EmployeeInfo saveEmployee(EmployeeInfo emp) { // TODO Auto-generated method stub hashOperations.putIfAbsent(hashReference,emp.getId(), emp); return emp; } @Override public EmployeeInfo getEmployeeInfo(Integer id) { // TODO Auto-generated method stub return hashOperations.get(hashReference, id); } @Override public Map<Integer, EmployeeInfo> getAllEmployeesInfo() { // TODO Auto-generated method stub return hashOperations.entries(hashReference); } }
With help of redis template, we create operation and able to see below.
API Calls through Postman:
Fetch data from Redis using MUST clause:
With the help of RedisInSight tool we can able to see the values
Install redis server and client in your local by using below link.
https://redis.io/download/
once done,
run both Server and client
you can able to see the value in client by using redis commands given below.
https://redis.io/docs/ui/cli/
You can install redisinsight by using below link.
https://redis.io/download/
https://redis.com/redis-enterprise/redis-insight/?_ga=2.158942330.1183401335.1682271749-1229249119.1682271749&_gl=1*1rbz76r*_ga*MTIyOTI0OTExOS4xNjgyMjcxNzQ5*_ga_8BKGRQKRPV*MTY4MjI3OTg0Ni4zLjEuMTY4MjI4MjE1NS42MC4wLjA.
Step to Config Database in RedisInSight
Click add database, add host and port, then click test connection
Then click add redis database.
Click the database, you can see keys and value. By change to java serialized
Able to see proper response.