Home  >  Spring 4

Spring 4 + Redis + Gradle Integration Annotation Example

By Arvind Rai, December 14, 2014
On this page we will learn spring 4 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 etc 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.

Software Required to Run Example

To run the example we need the following software.
1. JDK 6
2. Gradle
3. Eclipse
4. Redis-2.8

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 to Resolve JAR Dependency

Find the gradle build file to resolve JAR dependencies for redis and spring.
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'Concretepage'
version = '1.0-SNAPSHOT' 
repositories {
    maven { url "https://repo.spring.io/libs-release" }
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 i.e StringRedisTemplate in Spring to save and fetch keys and values.
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;
public class RedisConfig {
	public RedisConnectionFactory jedisConnectionFactory() {
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		JedisConnectionFactory ob = new JedisConnectionFactory(poolConfig);
		return ob;
	public StringRedisTemplate  stringRedisTemplate(){
		return new StringRedisTemplate(jedisConnectionFactory());

Main Class to Test Example

We should be aware with 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.
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();
	       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
	       //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.

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 4 + Redis + Gradle  Integration Annotation  Example
We are done now with a simple spring and redis example.

Download Source Code


©2022 concretepage.com | Privacy Policy | Contact Us