Spring + Redis + Gradle Integration Example

By Arvind Rai, November 29, 2023
On this page, we will learn Spring and redis integration with a simple example. Redis name has been taken from REmote DIctionary Server. Redis is a data-structure server which stores data as key/value. strings, hashes, lists, sets, sorted sets can be used to store data in redis data structure. To save and fetch data, we need RedisTemplate. String is more often used to store data in redis. So Spring provides a dedicated template that is StringRedisTemplate. Now find the annotation based example to integrate Spring Data and Redis.

Start with Redis Installation

To install redis, visit the URL http://redis.io/download. If operating system is Windows then visit the URL https://github.com/MSOpenTech/redis and download ZIP file. Extract in any location and navigate to redis-2.8\bin\release. Here we will get redis zip file. Extract it and we will get files like redis-cli and redis-server. Click on redis-server to start the server. A command prompt will open starting the server with default port 6379. Click on redis-cli that will open a command prompt using which we can run redis queries. To exercise redis query, visit the URL http://redis.io/commands.

Project Structure in Eclipse

Now we will move forward to integrate redis with spring. It will be helpful to check the project structure in eclipse for quick understanding.
Spring 4 + Redis + Gradle  Integration Annotation  Example

Gradle Build File

Find the Gradle build file to resolve JAR dependencies for Redis and Spring.
build.gradle
dependencies {
    compile  'org.springframework.boot:spring-boot-starter:1.2.0.RELEASE'
    compile  'org.springframework.boot:spring-boot-starter-redis:1.2.0.RELEASE'
    compile 'org.springframework.data:spring-data-commons:1.9.1.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-security:1.2.0.RELEASE'
} 

Configuration class for StringRedisTemplate

Most of the operations in redis are string based. So there is a dedicated class, StringRedisTemplate, in Spring to save and fetch keys and values.
RedisConfig.java
package com.concretepage.redis;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@EnableAutoConfiguration 
public class RedisConfig {
	@Bean
	public RedisConnectionFactory jedisConnectionFactory() {
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		poolConfig.setMaxTotal(5);
		poolConfig.setTestOnBorrow(true);
		poolConfig.setTestOnReturn(true);
		JedisConnectionFactory ob = new JedisConnectionFactory(poolConfig);
		ob.setUsePool(true);
		ob.setHostName("localhost");
		ob.setPort(6379);
		return ob;
	}
	@Bean
	public StringRedisTemplate  stringRedisTemplate(){
		return new StringRedisTemplate(jedisConnectionFactory());
	}
} 

Main Class to Test Application

Find some methods of StringRedisTemplate.
opsForValue: Returns the ValueOperations. ValueOperations has set and get method to save and fetch keys and values in redis.
opsForHash: Returns HashOperations that has methods like delete, put and get to save and fetch data in redis. Find the main class.
Main.java
package com.concretepage.redis;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;

public class Main {
	public static void main(String[] args) {
	       AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	       ctx.register(RedisConfig.class);
	       ctx.refresh();
	       StringRedisTemplate stringRedisTemplate  = ctx.getBean(StringRedisTemplate.class);
	       // Using set to set value
	       stringRedisTemplate.opsForValue().set("R", "Ram");
	       stringRedisTemplate.opsForValue().set("S", "Shyam");
	       //Fetch values from set
	       System.out.println(stringRedisTemplate.opsForValue().get("R"));
	       System.out.println(stringRedisTemplate.opsForValue().get("S"));
	       //Using Hash Operation
	       String mohan = "Mohan";
	       stringRedisTemplate.opsForHash().put("M", String.valueOf(mohan.hashCode()),mohan);
	       System.out.println(stringRedisTemplate.opsForHash().get("M", String.valueOf(mohan.hashCode())));
    }
} 
Output will be as given below.
Ram
Shyam
Mohan 

Run Query on Redis Cli

To check the persistence of data in redis we can run query too.
KEYS *: Prints the complete keys in redis.
MGET key: Prints the value of key persisted in redis.
Spring + Redis + Gradle  Integration Example

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us