DATAKV-23

+ String converter supports nulls
This commit is contained in:
Costin Leau
2011-01-24 17:23:26 +02:00
parent c1c61d1cc7
commit 31abc40a8b
3 changed files with 21 additions and 17 deletions

View File

@@ -69,8 +69,6 @@ import org.springframework.util.ClassUtils;
*/
public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperations<K, V> {
private static final byte[] EMPTY_ARRAY = new byte[0];
private boolean exposeConnection = false;
private RedisSerializer keySerializer = new JdkSerializationRedisSerializer();
private RedisSerializer valueSerializer = new JdkSerializationRedisSerializer();
@@ -281,16 +279,28 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
@SuppressWarnings("unchecked")
private byte[] rawKey(Object key) {
return (key != null ? keySerializer.serialize(key) : EMPTY_ARRAY);
Assert.notNull(key, "non null key required");
return keySerializer.serialize(key);
}
private byte[] rawString(String key) {
return (key != null ? stringSerializer.serialize(key) : EMPTY_ARRAY);
return stringSerializer.serialize(key);
}
@SuppressWarnings("unchecked")
private byte[] rawValue(Object value) {
return (value != null ? valueSerializer.serialize(value) : EMPTY_ARRAY);
return valueSerializer.serialize(value);
}
@SuppressWarnings("unchecked")
private <HK> byte[] rawHashKey(HK hashKey) {
Assert.notNull(hashKey, "non null hash key required");
return hashKeySerializer.serialize(hashKey);
}
@SuppressWarnings("unchecked")
private <HV> byte[] rawHashValue(HV value) {
return hashValueSerializer.serialize(value);
}
private byte[][] rawKeys(Collection<K> keys) {
@@ -317,17 +327,6 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
return rawKeys;
}
@SuppressWarnings("unchecked")
private <HK> byte[] rawHashKey(HK value) {
return (value != null ? hashKeySerializer.serialize(value) : EMPTY_ARRAY);
}
@SuppressWarnings("unchecked")
private <HV> byte[] rawHashValue(HV value) {
return (value != null ? hashValueSerializer.serialize(value) : EMPTY_ARRAY);
}
@SuppressWarnings("unchecked")
private <T extends Collection<V>> T deserializeValues(Collection<byte[]> rawValues, Class<? extends Collection> type) {
Collection<V> values = (List.class.isAssignableFrom(type) ? new ArrayList<V>(rawValues.size())

View File

@@ -32,6 +32,8 @@ import org.springframework.util.Assert;
*
* <b>Note:</b> The conversion service initialization happens automatically if the class is defined
* as a Spring bean.
*
* <b>Note:</b> Does not handle nulls in any special way delegating everything to the container.
*
* @author Costin Leau
*/

View File

@@ -25,11 +25,14 @@ import org.springframework.util.Assert;
* <p/>
* Useful when the interaction with the Redis happens mainly through Strings.
*
* <p/> Converts null into empty arrays (which get translated into empty strings on deserialization).
*
* @author Costin Leau
*/
public class StringRedisSerializer implements RedisSerializer<String> {
private final static byte[] EMPTY_ARRAY = new byte[0];
private final String EMPTY_STRING = "";
private final Charset charset;
public StringRedisSerializer() {
@@ -43,7 +46,7 @@ public class StringRedisSerializer implements RedisSerializer<String> {
@Override
public String deserialize(byte[] bytes) {
return (SerializerUtils.isEmpty(bytes) ? null : new String(bytes, charset));
return (SerializerUtils.isEmpty(bytes) ? EMPTY_STRING : new String(bytes, charset));
}
@Override