Spring Data Redis

2.1.2

Spring Data Redis, part of the larger Spring Data family, provides easy configuration and access to Redis from Spring applications. It offers both low-level and high-level abstractions for interacting with the store, freeing the user from infrastructural concerns.

Features

  • Connection package as low-level abstraction across multiple Redis drivers/connectors (Jedis and Lettuce. Support for JRedis and SRP is deprecated.)

  • Exception translation to Spring’s portable Data Access exception hierarchy for Redis driver exceptions

  • RedisTemplate that provides a high level abstraction for performing various Redis operations, exception translation and serialization support

  • Pubsub support (such as a MessageListenerContainer for message-driven POJOs)

  • Redis Sentinel and Redis Cluster support

  • JDK, String, JSON and Spring Object/XML mapping serializers

  • JDK Collection implementations on top of Redis

  • Atomic counter support classes

  • Sorting and Pipelining functionality

  • Dedicated support for SORT, SORT/GET pattern and returned bulk values

  • Redis implementation for Spring 3.1 cache abstraction

  • Automatic implementation of Repository interfaces including support for custom finder methods using @EnableRedisRepositories

  • CDI support for repositories

Configure RedisTemplate….

<bean id="jedisConnFactory"
    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
    p:use-pool="true"/>

<!-- redis template definition -->
<bean id="redisTemplate"
    class="org.springframework.data.redis.core.RedisTemplate"
    p:connection-factory-ref="jedisConnFactory"/>

Inject and use RedisTemplate or any of its opsForX() instances….

public class Example {

    // inject the actual template
    @Autowired
    private RedisTemplate<String, String> template;

    // inject the template as ListOperations
    // can also inject as Value, Set, ZSet, and HashOperations
    @Resource(name="redisTemplate")
    private ListOperations<String, String> listOps;

    public void addLink(String userId, URL url) {
        listOps.leftPush(userId, url.toExternalForm());
        // or use template directly
        redisTemplate.boundListOps(userId).leftPush(url.toExternalForm());
    }
}

Quick start

Bootstrap your application with Spring Initializr.

Documentation

Each Spring project has its own; it explains in great details how you can use project features and what you can achieve with them.
2.1.2 CURRENT GA Reference Doc. API Doc.
2.2.0 SNAPSHOT Reference Doc. API Doc.
2.1.3 SNAPSHOT Reference Doc. API Doc.
2.0.12 SNAPSHOT Reference Doc. API Doc.
2.0.11 GA Reference Doc. API Doc.
1.8.17 SNAPSHOT Reference Doc. API Doc.
1.8.16 GA Reference Doc. API Doc.