How to use Spring Data Redis for Storing Object in Redis In Memory Database

Posted By Vishal Kumar | 29-Jun-2017

Redis is driven by a key-store based data structure to persist the data and can be used as a database, cache and a message broker as well. Following are the steps of using redis in a Spring application.

Step 1: Maven Dependencies

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-core</artifactId>
     <version>4.1.5.RELEASE</version>
</dependency>
 
<dependency>
     <groupId>org.springframework.data</groupId>
     <artifactId>spring-data-redis</artifactId>
     <version>1.5.0.RELEASE</version>
</dependency>
 
<dependency>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-pool2</artifactId>
     <version>2.4.2</version>
</dependency>
 
<dependency>
     <groupId>redis.clients</groupId>
     <artifactId>jedis</artifactId>
     <version>2.8.0</version>
</dependency>

Step 2: Create a configuration file <SpringRadisConfig.java>

package redis.template;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class SpringRedisConfig {
@Bean
public JedisConnectionFactory connectionFactory() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
connectionFactory.setHostName("localhost");
connectionFactory.setPort(6379);
return connectionFactory;
}
 
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(connectionFactory());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
return redisTemplate;
}

Step 3: Create a class which object you want to store <Student.java>

package redis.template;
 
import java.io.Serializable;
 
public class Student implements Serializable{
 
/**
*/
private static final long serialVersionUID = 1L;
private long id;
private  String name;
 
public Student(long id, String name) {
this.id=id;
this.name=name;
}
 
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Step 4: Create a main class for storing the object <SpringRedisExample.java>

package redis.template;
 
import java.net.URISyntaxException;
 
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
 
public class Redis {
 
 
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(SpringRedisConfig.class);
@SuppressWarnings("unchecked")
RedisTemplate<String, Object> redisTemplate = (RedisTemplate<String, Object>) ctx.getBean("redisTemplate");
ValueOperations<String, Object> values = redisTemplate.opsForValue();
 
public void storeAndPrint() throws URISyntaxException, Exception {
 
try {
Student student = new Student(1L, "Vishal");
values.set("student", student);
Student std = values.get("student").get("std");
 
System.out.println(std.getId());
System.out.println(std.name());
 
} finally {
ctx.close();
}
}
public static void main(String[] args) throws URISyntaxException, Exception {
Redis example = new Redis();
example.storeAndPrint();
}
}

Conclusion: So redis is a very fast and in memory database it store data in key value pair. It is a cache database but nowadays it is used as a main database. Here we are using Jedis and RedisTemplate for storing data so you can store a map also at the place of object.

 

Note : Object stored in redis and objects retrieved from redis are not equal but the values are always same.

 

Thanks,

Request for Proposal

Recaptcha is required.

Sending message..