Spring Data Redis Example

As you’ve read about Redis Installation & Configuration, you’re mostly understand the main concept of Redis database. Redis is an extremely high-performed, lightweight data store. It provides key/value data access to persistent byte arrays, lists, sets and hash data structures. So, the insertion, deletion and retrieve operations inside Redis becomes very simple how we have worked with using of Map, List , Set and an Array data structures how it is used inside normal Java program; although some minimal modifications that you’ve probably never care about them.

As you’ve been doing once it comes to deal with any database platform, you have to download/install the database driver. For Redis four driver libraries are provided for allowing your application to get connected to Redis. Spring Data supports connecting to Redis using either the Jedis (That will be considered in this tutorial), JRedis, RJC or SRP driver libraries. It doesn’t matter which library have been used, cause the Spring Data has abstracted the differences between those drivers into common set of APIs and template-style helpers.

1. Spring Context Configuration

To connect to Redis using Jedis, you need to create an instance of org.springframework.data.redis.connection.jedis.JedisConnectionFactory. The other driver libraries have corresponding ConnectionFactory subclasses. Below the proper Spring Context (SpringContext.xml) that should be used for initializing and connecting the Redis database through using of Spring.

5. Spring Data Redis Example Application

The below Java Class, is just an executable application that developed for persisting an User entity associated with an Address inside Redis key/value database. Note that the using of this concept entity is just theoretical uses and it doesn’t mean anything when it comes to apply it inside the Redis.

6. Query Redis Persisted Store

Summary

Spring Data provides the Spring Developer an abstracted, simplest and modeled integrating way for communicating with the Redis database. Regardless of the driver library that the developer had used, Spring Data still able to operate upon Redis in a standard manner and that’s because the RedisTemplate Spring Data facility. By using the RedisTemplate, the developer is capable of invoking all of defined operations regarding the different persistent stores that provided by Redis.

About Amr Mohammed

Comments

I’m trying executing this sample and I’m getting the following exception.
How to make it work?

Exception in thread “main” java.lang.ClassCastException: com.datamodel.User cannot be cast to java.lang.String
at org.springframework.data.redis.serializer.StringRedisSerializer.serialize(StringRedisSerializer.java:32)
at org.springframework.data.redis.core.AbstractOperations.rawHashValue(AbstractOperations.java:166)
at org.springframework.data.redis.core.DefaultHashOperations.put(DefaultHashOperations.java:167)
at com.test.SpringJedisDataLayerTest.main(SpringJedisDataLayerTest.java:109)