使用Spring Cache + Redis + Jackson Serializer快取資料庫查詢結果中序列化問題的解決

來源:互聯網
上載者:User

標籤:request   redis   typename   else   cli   http   form   mes   inf   

應用情境

我們希望通過緩衝來減少對關係型資料庫的查詢次數,減輕資料庫壓力。在執行DAO類的select***(), query***()方法時,先從Redis中查詢有沒有快取資料,如果有則直接從Redis拿到結果,如果沒有再向資料庫發起查詢請求取資料。

序列化問題

要把domain object做為key-value對儲存在redis中,就必須要解決對象的序列化問題。Spring Data Redis給我們提供了一些現成的方案:

  • JdkSerializationRedisSerializer. 使用JDK提供的序列化功能。 優點是還原序列化時不需要提供類型資訊(class),但缺點是序列化後的結果非常龐大,是JSON格式的5倍左右,這樣就會消耗redis伺服器的大量記憶體。
  • Jackson2JsonRedisSerializer. 使用Jackson庫將對象序列化為JSON字串。優點是速度快,序列化後的字串短小精悍。但缺點也非常致命,那就是此類的建構函式中有一個型別參數,必須提供要序列化對象的類型資訊(.class對象)。 通過查看原始碼,發現其只在還原序列化過程中用到了類型資訊。

如果用方案一,就必須付出緩衝多佔用4倍記憶體的代價,實在承受不起。如果用方案二,則必須給每一種domain對象都配置一個Serializer,即如果我的應用程式裡有100種domain對象,那就必須在spring設定檔中配置100個Jackson2JsonRedisSerializer,這顯然是不現實的。

通過google, 發現spring data redis項目中有一個#145 pull request, 而這個提交請求的內容正是解決Jackson必須提供類型資訊的問題。然而不幸的是這個請求還沒有被merge。但我們可以把代碼copy一下放到自己的項目中:

/**
* @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:

<bean id="jacksonSerializer" class="com.fh.taolijie.component.GenericJackson2JsonRedisSerializer">
</bean>

重新構建部署,我們發現這個serializer可以同時支援多種不同類型的domain對象,問題解決。

使用Spring Cache + Redis + Jackson Serializer快取資料庫查詢結果中序列化問題的解決

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.