DATAREDIS-713 - Polishing.

Added static methods for common serializers (string, json, java) to RedisSerializer.

Original Pull Request: #288
This commit is contained in:
Christoph Strobl
2017-10-19 14:09:29 +02:00
parent 841d8ef447
commit 4d41beff31
16 changed files with 70 additions and 34 deletions

View File

@@ -24,7 +24,7 @@ import org.springframework.cache.support.NullValue;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -44,7 +44,7 @@ import org.springframework.util.ReflectionUtils;
*/
public class RedisCache extends AbstractValueAdaptingCache {
private static final byte[] BINARY_NULL_VALUE = new JdkSerializationRedisSerializer().serialize(NullValue.INSTANCE);
private static final byte[] BINARY_NULL_VALUE = RedisSerializer.java().serialize(NullValue.INSTANCE);
private final String name;
private final RedisCacheWriter cacheWriter;

View File

@@ -23,9 +23,8 @@ import org.springframework.cache.Cache;
import org.springframework.cache.interceptor.SimpleKey;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -95,8 +94,8 @@ public class RedisCacheConfiguration {
registerDefaultConverters(conversionService);
return new RedisCacheConfiguration(Duration.ZERO, true, true, null,
SerializationPair.fromSerializer(StringRedisSerializer.UTF_8),
SerializationPair.fromSerializer(new JdkSerializationRedisSerializer()), conversionService);
SerializationPair.fromSerializer(RedisSerializer.string()),
SerializationPair.fromSerializer(RedisSerializer.java()), conversionService);
}
/**

View File

@@ -117,7 +117,7 @@ public class DefaultStringRedisConnection implements StringRedisConnection, Deco
* @param connection Redis connection
*/
public DefaultStringRedisConnection(RedisConnection connection) {
this(connection, StringRedisSerializer.UTF_8);
this(connection, RedisSerializer.string());
}
/**

View File

@@ -96,7 +96,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
@SuppressWarnings("rawtypes") private @Nullable RedisSerializer valueSerializer = null;
@SuppressWarnings("rawtypes") private @Nullable RedisSerializer hashKeySerializer = null;
@SuppressWarnings("rawtypes") private @Nullable RedisSerializer hashValueSerializer = null;
private RedisSerializer<String> stringSerializer = StringRedisSerializer.UTF_8;
private RedisSerializer<String> stringSerializer = RedisSerializer.string();
private @Nullable ScriptExecutor<K> scriptExecutor;

View File

@@ -40,10 +40,10 @@ public class StringRedisTemplate extends RedisTemplate<String, String> {
* and {@link #afterPropertiesSet()} still need to be called.
*/
public StringRedisTemplate() {
setKeySerializer(StringRedisSerializer.UTF_8);
setValueSerializer(StringRedisSerializer.UTF_8);
setHashKeySerializer(StringRedisSerializer.UTF_8);
setHashValueSerializer(StringRedisSerializer.UTF_8);
setKeySerializer(RedisSerializer.string());
setValueSerializer(RedisSerializer.string());
setHashKeySerializer(RedisSerializer.string());
setHashValueSerializer(RedisSerializer.string());
}
/**

View File

@@ -127,7 +127,7 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab
private final SubscriptionTask subscriptionTask = new SubscriptionTask();
private volatile RedisSerializer<String> serializer = StringRedisSerializer.UTF_8;
private volatile RedisSerializer<String> serializer = RedisSerializer.string();
private long recoveryInterval = DEFAULT_RECOVERY_INTERVAL;
@@ -307,7 +307,7 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab
* @param connectionFactory The connectionFactory to set.
*/
public void setConnectionFactory(RedisConnectionFactory connectionFactory) {
Assert.notNull(connectionFactory, "ConnectionFactory must not be null!");
this.connectionFactory = connectionFactory;
}

View File

@@ -318,8 +318,8 @@ public class MessageListenerAdapter implements InitializingBean, MessageListener
* @see JdkSerializationRedisSerializer
*/
protected void initDefaultStrategies() {
setSerializer(StringRedisSerializer.UTF_8);
setStringSerializer(StringRedisSerializer.UTF_8);
setSerializer(RedisSerializer.string());
setStringSerializer(RedisSerializer.string());
}
/**

View File

@@ -98,7 +98,7 @@ class DefaultRedisSerializationContext<K, V> implements RedisSerializationContex
private @Nullable SerializationPair<V> valueTuple;
private @Nullable SerializationPair<?> hashKeyTuple;
private @Nullable SerializationPair<?> hashValueTuple;
private SerializationPair<String> stringTuple = SerializationPair.fromSerializer(StringRedisSerializer.UTF_8);
private SerializationPair<String> stringTuple = SerializationPair.fromSerializer(RedisSerializer.string());
/* (non-Javadoc)
* @see org.springframework.data.redis.serializer.RedisSerializationContextBuilder#key(SerializationPair)

View File

@@ -93,7 +93,7 @@ public interface RedisSerializationContext<K, V> {
* @return
*/
static RedisSerializationContext<String, String> string() {
return fromSerializer(StringRedisSerializer.UTF_8);
return fromSerializer(RedisSerializer.string());
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2013 the original author or authors.
* Copyright 2011-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@ import org.springframework.lang.Nullable;
* Basic interface serialization and deserialization of Objects to byte arrays (binary data). It is recommended that
* implementations are designed to handle null objects/empty arrays on serialization and deserialization side. Note that
* Redis does not accept null keys or values but can return null replies (for non existing keys).
*
*
* @author Mark Pollack
* @author Costin Leau
* @author Christoph Strobl
@@ -45,4 +45,37 @@ public interface RedisSerializer<T> {
*/
@Nullable
T deserialize(@Nullable byte[] bytes) throws SerializationException;
/**
* Obtain a {@link RedisSerializer} using java serialization.<br />
* <strong>Note:</strong> Ensure that your domain objects are actually {@link java.io.Serializable serializable}.
*
* @return never {@literal null}.
* @since 2.1
*/
static RedisSerializer<Object> java() {
return new JdkSerializationRedisSerializer();
}
/**
* Obtain a {@link RedisSerializer} that can read and write JSON using
* <a href="https://github.com/FasterXML/jackson-core">Jackson</a>.
*
* @return never {@literal null}.
* @since 2.1
*/
static RedisSerializer<Object> json() {
return new GenericJackson2JsonRedisSerializer();
}
/**
* Obtain a simple {@link java.lang.String} to {@literal byte[]} (and back) serializer using
* {@link java.nio.charset.StandardCharsets#UTF_8 UTF-8} as the default {@link java.nio.charset.Charset}.
*
* @return never {@literal null}.
* @since 2.1
*/
static RedisSerializer<String> string() {
return StringRedisSerializer.UTF_8;
}
}

View File

@@ -22,8 +22,8 @@ import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Simple {@literal} to {@literal byte[]} (and back) serializer. Converts Strings into bytes and vice-versa using the
* specified charset (by default UTF-8).
* Simple {@link java.lang.String} to {@literal byte[]} (and back) serializer. Converts {@link java.lang.String Strings}
* into bytes and vice-versa using the specified charset (by default {@literal UTF-8}).
* <p>
* Useful when the interaction with the Redis happens mainly through Strings.
* <p>
@@ -38,22 +38,26 @@ public class StringRedisSerializer implements RedisSerializer<String> {
private final Charset charset;
/**
* Serializer to use 7 bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.
*
* {@link StringRedisSerializer} to use 7 bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode
* character set.
*
* @see StandardCharsets#US_ASCII
* @since 2.1
*/
public static final StringRedisSerializer US_ASCII = new StringRedisSerializer(StandardCharsets.US_ASCII);
/**
* Serializer to use ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
*
* {@link StringRedisSerializer} to use ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
*
* @see StandardCharsets#ISO_8859_1
* @since 2.1
*/
public static final StringRedisSerializer ISO_8859_1 = new StringRedisSerializer(StandardCharsets.ISO_8859_1);
/**
* Serializer to use 8 bit UCS Transformation Format.
*
* {@link StringRedisSerializer} to use 8 bit UCS Transformation Format.
*
* @see StandardCharsets#UTF_8
* @since 2.1
*/
public static final StringRedisSerializer UTF_8 = new StringRedisSerializer(StandardCharsets.UTF_8);

View File

@@ -79,7 +79,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
Assert.notNull(factory, "a valid factory is required");
RedisTemplate<String, Double> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(StringRedisSerializer.UTF_8);
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Double.class));
redisTemplate.setExposeConnection(true);
redisTemplate.setConnectionFactory(factory);

View File

@@ -102,7 +102,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
private RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory, @Nullable Integer initialValue) {
RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(StringRedisSerializer.UTF_8);
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Integer.class));
redisTemplate.setExposeConnection(true);
redisTemplate.setConnectionFactory(factory);

View File

@@ -79,7 +79,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
Assert.notNull(factory, "a valid factory is required");
RedisTemplate<String, Long> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(StringRedisSerializer.UTF_8);
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Long.class));
redisTemplate.setExposeConnection(true);
redisTemplate.setConnectionFactory(factory);