@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public CacheManager cacheManager(RedisTemplate, ?> redisTemplate) {
RedisCacheManager manager = new RedisCacheManager(redisTemplate);
return manager;
}
@Bean
public RedisTemplate, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(connectionFactory);
setMySerializer(template);
template.afterPropertiesSet();
LogCore.BASE.info("template{}" ,ReflectionToStringBuilder.toString(template, ToStringStyle.SHORT_PREFIX_STYLE));
return template;
}
/**
*
*/
private void setMySerializer(RedisTemplate template) {
Jackson2JsonRedisSerializer
最後にSpring data redis定義のシーケンス化インタフェースとデフォルトのJDKシーケンス化のパッケージを見てみましょう.コードは比較的きれいで、そこから学ぶことができます. [API]
public interface RedisSerializer {
byte[] serialize(T t) throws SerializationException;
T deserialize(byte[] bytes) throws SerializationException;
}
[API]
public class JdkSerializationRedisSerializer implements RedisSerializer {
private Converter serializer = new SerializingConverter();
private Converter deserializer = new DeserializingConverter();
public Object deserialize(byte[] bytes) {
if (SerializationUtils.isEmpty(bytes)) {
return null;
}
try {
return deserializer.convert(bytes);
} catch (Exception ex) {
throw new SerializationException("Cannot deserialize", ex);
}
}
public byte[] serialize(Object object) {
if (object == null) {
return SerializationUtils.EMPTY_ARRAY;
}
try {
return serializer.convert(object);
} catch (Exception ex) {
throw new SerializationException("Cannot serialize", ex);
}
}
}
[API]
public interface Converter {
T convert(S source);
}
[API]
public class SerializingConverter implements Converter {
private final Serializer serializer;
public SerializingConverter() {
this.serializer = new DefaultSerializer();
}
public SerializingConverter(Serializer serializer) {
Assert.notNull(serializer, "Serializer must not be null");
this.serializer = serializer;
}
@Override
public byte[] convert(Object source) {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024);
try {
this.serializer.serialize(source, byteStream);
return byteStream.toByteArray();
}
catch (Throwable ex) {
throw new SerializationFailedException("Failed to serialize object using " +
this.serializer.getClass().getSimpleName(), ex);
}
}
}
[API]
public class DeserializingConverter implements Converter {
private final Deserializer deserializer;
public DeserializingConverter() {
this.deserializer = new DefaultDeserializer();
}
public DeserializingConverter(ClassLoader classLoader) {
this.deserializer = new DefaultDeserializer(classLoader);
}
public DeserializingConverter(Deserializer deserializer) {
Assert.notNull(deserializer, "Deserializer must not be null");
this.deserializer = deserializer;
}
@Override
public Object convert(byte[] source) {
ByteArrayInputStream byteStream = new ByteArrayInputStream(source);
try {
return this.deserializer.deserialize(byteStream);
}
catch (Throwable ex) {
throw new SerializationFailedException("Failed to deserialize payload. " +
"Is the byte array a result of corresponding serialization for " +
this.deserializer.getClass().getSimpleName() + "?", ex);
}
}
}
[API]シーケンス化writeObject
public class DefaultSerializer implements Serializer {
@Override
public void serialize(Object object, OutputStream outputStream) throws IOException {
if (!(object instanceof Serializable)) {
throw new IllegalArgumentException(getClass().getSimpleName() + " requires a Serializable payload " +
"but received an object of type [" + object.getClass().getName() + "]");
}
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(object);
objectOutputStream.flush();
}
}
[API]逆シーケンス化はクラスローダで
public class DefaultDeserializer implements Deserializer {
private final ClassLoader classLoader;
public DefaultDeserializer() {
this.classLoader = null;
}
public DefaultDeserializer(ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
@SuppressWarnings("resource")
public Object deserialize(InputStream inputStream) throws IOException {
ObjectInputStream objectInputStream = new ConfigurableObjectInputStream(inputStream, this.classLoader);
try {
return objectInputStream.readObject();
}
catch (ClassNotFoundException ex) {
throw new NestedIOException("Failed to deserialize object type", ex);
}
}
}