SpringSource Releases Spring Data Redis 1.0.0
SpringSource has released the first stable version of an open source library that allows for easy integration of Redis in Java applications. Redis is a key value store also sponsored by VMWare/SpringSource and used by high profile sites such as GitHub and StackOverflow.
Redis is one of the emerging NoSQL datastores that focuses on simplicity and performance (the whole dataset is kept in memory). It is accessed using an extensive set of commands and unlike many other key-value stores it supports additional datatypes aside from basic Strings. Some example commands look like this:
set my-news-site INFOQ =>OK get my-news-site =>"INFOQ" set my-counter 22 =>OK get my-counter =>"22" incr my-counter =>(integer) 23 get my-counter =>"23"
While Redis itself is written in ANSI C it comes with clients in several programming languages including Haskell, Tcl, Go and SmallTalk, as well as C# and Java. For integration with Java there are the following libraries:
The wealth of options shows a healthy involvement from the community but selecting the appropriate library for development can be time consuming. Each library comes with its own set of APIs, configuration and documentation. Choosing one, and later deciding to upgrade to a different library is not straight-forward.
Spring Data Redis is part of the Spring Data umbrella project that aims to promote the use of non-relational databases in Spring Environments (as already done for JPA/Hibernate). Its major goals are portability and consistency in Spring Java applications. At its lowest level, Spring Data Redis provides a basic abstraction over the individual Java Redis libraries. Developers can target the unified Spring Redis API and easily switch among implementations (e.g. for performance reasons)
Therefore Spring Data Redis offers RedisConnection, an abstraction for a short-lived non-thread-safe connection to a Redis instance (similar to the JPA EntityManager or the Hibernate Session) and RedisConnectionFactory which creates such connections (similar to a JPA EntityManagerFactory or the Hibernate SessionFactory). These abstractions hide the actual implementation mechanism over a common programming interface allowing for easy portability among the Java Redis libraries supported by Spring.
The RedisConnection contains all Redis commands but with Java syntax. The full power of Redis can be accessed via this interface using the low level commands of key saving/retrieving. The example shown above would be written using the Java set, get and incr commands provided by RedisConnection. The big advantage here is that these commands stay the same regardless of the actual Java library used. Changing to a different implementation is isolated to a single configuration line in the Spring Context:
<!-- Jedis ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"/> <!-- JRedis ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jredis.JredisConnectionFactory"/> <!-- RJC ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.rjc.RjcConnectionFactory"/>
At a higher level Spring Data Redis comes with a RedisTemplate that can be used to store entire Java Objects directly into Redis instead of just basic types (i.e. Strings and numbers). Out of the box the following serialization methods are supported:
- JDK serialization (the default method)
- JSON (via the Jackson library)
- XML (via Spring OXM module)