From d2d59a17e5ac197d79153619f34c4bfe76871e57 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Tue, 1 Feb 2011 20:17:45 +0200 Subject: [PATCH] add docs on StringConnection/Template and the serializers --- src/docbkx/reference/redis.xml | 90 +++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 24 deletions(-) diff --git a/src/docbkx/reference/redis.xml b/src/docbkx/reference/redis.xml index ecd8575dd..4e0e7858c 100644 --- a/src/docbkx/reference/redis.xml +++ b/src/docbkx/reference/redis.xml @@ -4,7 +4,7 @@ Redis support - One of the key value stores supported by SDKV is Redis. + One of the key value stores supported by SDKV is Redis. To quote the project home page: Redis is an advanced key-value store. It is similar to memcached but the dataset is not volatile, and values can be strings, @@ -18,8 +18,7 @@
Redis Requirements - SDKV requires Redis 2.0 or above (work is underway to support the upcoming (at the time this document was written) 2.2) and - Java SE 6.0 or above. + SDKV requires Redis 2.0 or above (Redis 2.2 is recommended) and Java SE 6.0 or above. In terms of language bindings (or connectors), SDKV integrates with Jedis and JRedis, two popular open source Java libraries for Redis. If you are aware of any other connector that we should be integrating is, please send us feedback. @@ -67,7 +66,7 @@ Active RedisConnection are created through RedisConnectionFactory. In addition, the factories act as PersistenceExceptionTranslator meaning once declared, allow one to do transparent exception translation for example through the use of the @Repository annotation and AOP. For more information see the dedicated - section in Spring Framework documentation. + section in Spring Framework documentation. Depending on the underlying configuration, the factory can return a new connection or an existing connection (in case a pool is used).
@@ -101,7 +100,7 @@ ]]> - For intense use however, one might want to enable connection pooling or set a certain host or password: + For production use however, one might want to tweak the settings such as the host or password: + p:host-name="server" p:port="6379"/> ]]> @@ -134,7 +133,7 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> + p:host-name="server" p:port="6379"/> ]]> As one can note, the configuration is quite similar to the Jedis one. @@ -158,7 +157,7 @@ The template offers a high-level abstraction for Redis interaction - while RedisConnection offer low level methods that accept and return binary values (byte arrays), the template takes care of serialization and connection management, freeing the user from dealing with such details.
- Moreover, the template provides operations views (following the grouping from Redis command reference) + Moreover, the template provides operations views (following the grouping from Redis command reference) that offer rich, generified interfaces for working against a certain type or certain key (through the KeyBound interfaces) as described below: @@ -229,11 +228,53 @@ Out of the box, RedisTemplate uses a Java-based serializer for most of its operations. This means that any object written or read by the template will be serializer/deserialized through Java. The serialization mechanism can be easily changed on the template and the Redis module offers several implementations available in the - org.springframework.data.keyvalue.redis.serializer package. Note that the template requires all keys to be non-null - values can be null as long as the underlying + org.springframework.data.keyvalue.redis.serializer package - see for more information. + Note that the template requires all keys to be non-null - values can be null as long as the underlying serializer accepts them; read the javadoc of each serializer for more information. - Since it's quite the keys and values stored in Redis can be java.lang.String, the Redis modules provides StringRedisTemplate, - a convenient provides a one-stop solution for intensive operations operations. In addition to be bound to String keys, the template uses the + For cases where a certain template view is needed, one the view as a dependency and inject the template: the container will automatically perform the conversion + eliminating the opsFor[X] calls: + + + + + + + + + + ... +]]> + + template; + + // inject the template as ListOperations + @Autowired + private ListOperations listOps; + + public void addLink(String userId, URL url) { + listOps.leftPush(userId, url.toExternalForm()); + } +}]]> + + +
+ String-focused convenience classes + + Since it's quite the keys and values stored in Redis can be java.lang.String, the Redis modules provides two extensions to RedisConnection + and RedisTemplate respectively the StringRedisConnection (and its DefaultStringRedisConnection implementation) + and StringRedisTemplate as a convenient one-stop solution + for intensive String operations. In addition to be bound to String keys, the template and the connection use the StringRedisSerializer underneath which means the stored keys and values are human readable (assuming the same encoding is used both in Redis and your code). For example: @@ -264,19 +305,6 @@ } }]]> - For cases where a certain template view is needed, one the view as a dependency and inject the template: the container will automatically perform the conversion - eliminating the opsFor[X] calls: - - listOps; - - public void addLink(String userId, URL url) { - listOps.leftPush(userId, url.toExternalForm()); - } -}]]> As with the other Spring templates, RedisTemplate and StringRedisTemplate allow the developer to talk directly to Redis through the RedisCallback interface: this gives complete control to the developer as it talks directly to the RedisConnection. @@ -293,6 +321,20 @@ }]]>
+
+ Serializers + + From the framework perspective, the data stored in Redis are just bytes. While Redis itself supports various types, for the most part these refer to the way the data is stored + rather then what it represents. It is up to the user to decide whether the information gets translated into Strings or any other objects. The conversion between the user (custom) + types and raw data (and vice-versa) is handled in SDKV Redis through the RedisSerializer interface + (package org.springframework.data.keyvalue.redis.serializer) which as the name implies, takes care of the serialization process. Multiple implementations are + available out of the box, two of which have been already mentioned before in this documentation: the StringRedisSerializer and + the JdkSerializationRedisSerializer. However one can use OxmSerializer for Object/XML mapping through Spring 3 + OXM support or JacksonJsonRedisSerializer for storing + data in JSON format. Do note that the storage format is not limited only to values - it can be used for keys, values or hashes + without any restrictions. +
+