咱們但願經過緩存來減小對關係型數據庫的查詢次數,減輕數據庫壓力。在執行DAO類的select***()
, query***()
方法時,先從Redis
中查詢有沒有緩存數據,若是有則直接從Redis
拿到結果,若是沒有再向數據庫發起查詢請求取數據。css
要把domain object作爲key-value對保存在redis中,就必需要解決對象的序列化問題。Spring Data Redis給咱們提供了一些現成的方案:java
JdkSerializationRedisSerializer
. 使用JDK提供的序列化功能。 優勢是反序列化時不須要提供類型信息(class
),但缺點是序列化後的結果很是龐大,是JSON格式的5倍左右,這樣就會消耗redis服務器的大量內存。Jackson2JsonRedisSerializer
. 使用Jackson
庫將對象序列化爲JSON字符串。優勢是速度快,序列化後的字符串短小精悍。但缺點也很是致命,那就是此類的構造函數中有一個類型參數,必須提供要序列化對象的類型信息(.class
對象)。 經過查看源代碼,發現其只在反序列化過程當中用到了類型信息。若是用方案一,就必須付出緩存多佔用4倍內存的代價,實在承受不起。若是用方案二,則必須給每一種domain對象都配置一個Serializer,即若是個人應用裏有100種domain對象,那就必須在spring配置文件中配置100個Jackson2JsonRedisSerializer
,這顯然是不現實的。git
經過google, 發現spring data redis項目中有一個#145 pull request, 而這個提交請求的內容正是解決Jackson
必須提供類型信息的問題。然而不幸的是這個請求尚未被merge
。但咱們能夠把代碼copy一下放到本身的項目中:github
/**
* @author Christoph Strobl
* @since 1.6
*/
public class GenericJackson2JsonRedisSerializer implements RedisSerializer<Object> {
private final ObjectMapper mapper;
/**
* Creates {@link GenericJackson2JsonRedisSerializer} and configures {@link ObjectMapper} for default typing.
*/
public GenericJackson2JsonRedisSerializer() {
this((String) null);
}
/**
* Creates {@link GenericJackson2JsonRedisSerializer} and configures {@link ObjectMapper} for default typing using the
* given {@literal name}. In case of an {@literal empty} or {@literal null} String the default
* {@link JsonTypeInfo.Id#CLASS} will be used.
*
* @param classPropertyTypeName Name of the JSON property holding type information. Can be {@literal null}.
*/
public GenericJackson2JsonRedisSerializer(String classPropertyTypeName) {
this(new ObjectMapper());
if (StringUtils.hasText(classPropertyTypeName)) {
mapper.enableDefaultTypingAsProperty(DefaultTyping.NON_FINAL, classPropertyTypeName);
} else {
mapper.enableDefaultTyping(DefaultTyping.NON_FINAL, As.PROPERTY);
}
}
/**
* Setting a custom-configured {@link ObjectMapper} is one way to take further control of the JSON serialization
* process. For example, an extended {@link SerializerFactory} can be configured that provides custom serializers for
* specific types.
*
* @param mapper must not be {@literal null}.
*/
public GenericJackson2JsonRedisSerializer(ObjectMapper mapper) {
Assert.notNull(mapper, "ObjectMapper must not be null!");
this.mapper = mapper;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.serializer.RedisSerializer#serialize(java.lang.Object)
*/
@Override
public byte[] serialize(Object source) throws SerializationException {
if (source == null) {
return SerializationUtils.EMPTY_ARRAY;
}
try {
return mapper.writeValueAsBytes(source);
} catch (JsonProcessingException e) {
throw new SerializationException("Could not write JSON: " + e.getMessage(), e);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.serializer.RedisSerializer#deserialize(byte[])
*/
@Override
public Object deserialize(byte[] source) throws SerializationException {
return deserialize(source, Object.class);
}
/**
* @param source can be {@literal null}.
* @param type must not be {@literal null}.
* @return {@literal null} for empty source.
* @throws SerializationException */
public <T> T deserialize(byte[] source, Class<T> type) throws SerializationException {
Assert.notNull(type,
"Deserialization type must not be null! Pleaes provide Object.class to make use of Jackson2 default typing.");
if (SerializationUtils.isEmpty(source)) {
return null;
}
try {
return mapper.readValue(source, type);
} catch (Exception ex) {
throw new SerializationException("Could not read JSON: " + ex.getMessage(), ex);
}
}
}
而後在配置文件中使用這個GenericJackson2JsonRedisSerializer
:redis
<bean id="jacksonSerializer" class="com.fh.taolijie.component.GenericJackson2JsonRedisSerializer">
</bean>
從新構建部署,咱們發現這個serializer能夠同時支持多種不一樣類型的domain對象,問題解決。spring