Home » Enterprise Java » spring » Spring Data Redis Example

About Chandan Singh

Chandan Singh
Chandan holds a degree in Computer Engineering and is a passionate software programmer. He has good experience in Java/J2EE Web-Application development for Banking and E-Commerce Domains.

Spring Data Redis Example

In the past few examples, we have been integrating Spring Data with the NoSql Databases. In this example, we shall integrate Spring Data with Redis, a key-value based NoSQL Database.

Spring Data offers a level of abstraction over the traditional way of executing query by exposing a Repository. As such, the user need not write queries and call specific methods, depending upon the underlying Database.

Redis employs a key-store Data Structure to store data. It can be used to store complex data structure like List, Set, Hashes etc, which is why it is also referred to as Data-Structure Server. Like Gemfire, Redis too uses in-memory datasets for quicker access.

Want to master Spring Data ?

Subscribe to our newsletter and download the Spring Data Ultimate Guide right now!

In order to help you master Spring Data, we have compiled a kick-ass guide with all the major features and use cases! Besides studying them online you may download the eBook in PDF format!

 

1. Implementation

Redis can be downloaded from here for Linux systems. We are using Redis 3.0.3 for this demonstration. Once the Redis Server is up and running we can start connecting to it through Spring Data.

Then, we need to have following JAR Files to connect to Redis Server:

  • commons-logging.jar
  • commons-pool.jar
  • jackson-core-asl.jar
  • jackson-mapper.jar
  • jedis.jar
  • spring-asm.jar
  • spring-beans.jar
  • spring-context.jar
  • spring-core.jar
  • spring-data-redis.jar
  • spring-expression.jar
  • spring-tx.jar

Create a project in eclipse or any IDE and add the JAR files downloaded above. Now that the project is setup, we start with the coding phase :

First, we create an Entity that is to be persisted in the Redis Database.

Person.java


package com.jcg.examples.bean;

import java.io.Serializable;

public class Person implements Serializable
{

	private static final long serialVersionUID = -8243145429438016231L;

	public enum Gender{Male, Female}
	
	private String id;
	
	private String name;
	
	private Gender gender;
	
	private int age;

	public String getId()
	{
			return id;
	}

	public void setId(String id)
	{
			this.id = id;
	}

	public String getName()
	{
			return name;
	}

	public void setName(String name)
	{
			this.name = name;
	}

	public Gender getGender()
	{
			return gender;
	}

	public void setGender(Gender gender)
	{
			this.gender = gender;
	}

	public int getAge()
	{
			return age;
	}

	public void setAge(int age)
	{
			this.age = age;
	}
	
	@Override
	public int hashCode()
	{
			final int prime = 31;
			int result = 1;
			result = prime * result + age;
			result = prime * result + ((gender == null) ? 0 : gender.hashCode());
			result = prime * result + ((id == null) ? 0 : id.hashCode());
			result = prime * result + ((name == null) ? 0 : name.hashCode());
			return result;
	}

	@Override
	public boolean equals(Object obj)
	{
			if (this == obj)
					return true;
			if (obj == null)
					return false;
			if (getClass() != obj.getClass())
					return false;
			Person other = (Person) obj;
			if (age != other.age)
					return false;
			if (gender == null)
			{
					if (other.gender != null)
							return false;
			}
			else if (!gender.equals(other.gender))
					return false;
			if (id == null)
			{
					if (other.id != null)
							return false;
			}
			else if (!id.equals(other.id))
					return false;
			if (name == null)
			{
					if (other.name != null)
							return false;
			}
			else if (!name.equals(other.name))
					return false;
			return true;
	}

	@Override
	public String toString()
	{
			return "Person [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + "]";
	}	
	
}

One difference here from the previous confgiuration of PoJos the reader will find is that there is no configuration information provided in the Entity. The Data-base simply serializes and stores the PoJo against the Key passed. That is why it is important to implement the Serializable interface. Not implementing Serializable interface leads to silly Serialization Exceptions at the time of persisting to the Database.

Next, we configure the repository which will help us in persisting the PoJo to the Redis Server:

PersonRepo.java


package com.jcg.examples.repo;

import java.util.Map;

import com.jcg.examples.bean.Person;

public interface PersonRepo 
{
	public void save(Person person);
	
	public Person find(String id);
	
	public Map<Object, Object> findAll();
	
	public void delete(String id);
}

Next, we implement PersonRepo in the PersonRepoImpl class:

PersonRepoImpl.java


package com.jcg.examples.repo.impl;

import java.util.Map;

import org.springframework.data.redis.core.RedisTemplate;

import com.jcg.examples.bean.Person;
import com.jcg.examples.repo.PersonRepo;

public class PersonRepoImpl implements  PersonRepo
{
	
	private RedisTemplate<String, Person> redisTemplate;
	
	private static String PERSON_KEY = "Person";

	public RedisTemplate<String, Person> getRedisTemplate()
	{
			return redisTemplate;
	}

	public void setRedisTemplate(RedisTemplate<String, Person> redisTemplate)
	{
		this.redisTemplate = redisTemplate;
	}

	@Override
	public void save(Person person)
	{
		this.redisTemplate.opsForHash().put(PERSON_KEY, person.getId(), person);
	}

	@Override
	public Person find(String id)
	{
		return (Person)this.redisTemplate.opsForHash().get(PERSON_KEY, id);
	}

	@Override
	public Map<Object,Object> findAll()
	{
		return this.redisTemplate.opsForHash().entries(PERSON_KEY);
	}

	@Override
	public void delete(String id)
	{
		this.redisTemplate.opsForHash().delete(PERSON_KEY,id);	
	}

}

PersonRepoImpl uses the RedisTemplate to communicate with the Redis Server. Since we are using Hash based Operations, we are using the Redistemplate#opsForHash(). The method returns an instance of HashOperations class. We use the methods in this class to store retrieve the Keys.

Next is XML configuration.

spring-config.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx", 
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- Redis Connection Factory -->
	<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
			p:use-pool="true" />
			
	<!-- Redis Template Configuration-->
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
			p:connection-factory-ref="jedisConnFactory" />
			
	<bean id="personRepo" class="com.jcg.examples.repo.impl.PersonRepoImpl">
		<property name="redisTemplate" ref="redisTemplate" />	
	</bean>

</beans>

The RedisTemplate injected into the PersonRepoImpl class by the Spring BeanFactory. The RedisTemplate requires the JedisConnectionFactory instance from the Jedis JAR. Next we inject the RedisTemplate instance into the PersonRepoImpl bean as a reference. We can also use @Autowired to configure the same and add the component-scan directive in the XML.

Now that all is set, let’s run the application and test out the code! Here’s Application class that loads the XML file to instantiate the Spring Container and run CRUD commands on the server.

Application.java


package com.jcg.examples.test;


import java.util.Map;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

import com.jcg.examples.bean.Person;
import com.jcg.examples.bean.Person.Gender;
import com.jcg.examples.repo.PersonRepo;


public class Application
{
	public static void main(String[] args)
	{
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new ClassPathResource("resources/spring-config.xml").getPath());
		PersonRepo personRepo = (PersonRepo)context.getBean("personRepo");
		
		Person person = new Person();
		person.setId("1");
		person.setAge(55);
		person.setGender(Gender.Female);
		person.setName("Oracle");
		
		personRepo.save(person);
		
		Person person2 = new Person();
		person2.setId("2");
		person2.setAge(60);
		person2.setGender(Gender.Male);
		person2.setName("TheArchitect");
		
		personRepo.save(person2);
		
		Person person3 = new Person();
		person3.setId("3");
		person3.setAge(25);
		person3.setGender(Gender.Male);
		person3.setName("TheOne");
		
		personRepo.save(person3);
		
		System.out.println("Finding the One : "+personRepo.find("3"));
		
		Map <Object,Object> personMatrixMap = personRepo.findAll();
		
		System.out.println("Currently in the Redis Matrix");
	
		System.out.println(personMatrixMap);
		
		System.out.println("Deleting The Architect ");
		
		personRepo.delete("2");
		
		personMatrixMap = personRepo.findAll();
		
		System.out.println("Remnants .. : ");
		
		System.out.println(personMatrixMap);
		
		context.close();

	}
}

In Application class, we are creating an instances of Person Class and save them into the Redis database. We can then retrieve and delete them.

Here’s the sample output of the program :


Aug 09, 2015 4:02:57 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@42b1b290: startup date [Sun Aug 09 16:02:57 IST 2015]; root of context hierarchy
Aug 09, 2015 4:02:57 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [resources/spring-config.xml]
Aug 09, 2015 4:02:57 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@322558e: defining beans [jedisConnFactory,redisTemplate,personRepo]; root of factory hierarchy
Finding the One : Person [id=3, name=TheOne, gender=Male, age=25]
Currently in the Redis Matrix
{1=Person [id=1, name=Oracle, gender=Female, age=55], 3=Person [id=3, name=TheOne, gender=Male, age=25], 2=Person [id=2, name=TheArchitect, gender=Male, age=60]}
Deleting The Architect 
Remnants .. : 
{1=Person [id=1, name=Oracle, gender=Female, age=55], 3=Person [id=3, name=TheOne, gender=Male, age=25]}
Aug 09, 2015 4:02:58 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@42b1b290: startup date [Sun Aug 09 16:02:57 IST 2015]; root of context hierarchy
Aug 09, 2015 4:02:58 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@322558e: defining beans [jedisConnFactory,redisTemplate,personRepo]; root of factory hierarchy

2. Download the Source Code

Here we demonstrated how to configure and manage a Redis Data Repository using Spring Data.

Download
You can download the source code of this example here: SpringDataRedisExample.zip
(No Ratings Yet)
1 Comment Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Kumar S Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Kumar S
Guest

Hmmm…interesting list. I however was thinking that you may want to include Engati as well. Engati is a chatbot platform that allows you to build, manage, integrate, train, analyse and publish your personalized bot in a matter of minutes. It presently supports eight major messaging platforms including messenger, kik, telegram, line, viber, skype, slack and webchat with a focus on customer engagement, conversational commerce, and customer service and fulfillments.
Read more about it here http://www.engati.com/?utm_source=backlinks