Reputation: 2133
I have a Java web service that uses Redis for caching. Initially I created a CacheService that directly accessed the Redisson client in order to handle caching. I recently refactored the cache handling to use the Spring Cache abstraction, which made the code a lot cleaner and encouraged modular design. Unfortunately Spring uses Jackson to serialize/deserialize the cached objects, resulting in the cached values being much larger than before due to type info being stored in the JSON. This caused an unacceptable increase in response time in reads from the cache. Is there any way to customize the way that Spring serializes and deserializes the cached content? I'd like to replace it with my own logic, but don't see anything in the docs. I'd rather not have to roll my own AspectJ cache implementation if possible.
Upvotes: 1
Views: 7178
Reputation: 10813
Redisson also provides Spring Cache integration. It supports many popular codecs: Jackson JSON, Avro, Smile, CBOR, MsgPack, Kryo, FST, LZ4, Snappy and JDK Serialization.
Here is an example:
@Bean
CacheManager cacheManager(RedissonClient redissonClient) {
Codec codec = new JsonJacksonCodec();
Map<String, CacheConfig> config = new HashMap<String, CacheConfig>();
config.put("testMap", new CacheConfig(24*60*1000, 12*60*1000));
return new RedissonSpringCacheManager(redissonClient, config, codec);
}
Upvotes: 2
Reputation: 33151
The RedisCacheManager
takes a RedisOperations
and you can configure there how serialization works. You can tune serialization for keys and values though I suspect key should use StringRedisSerializer
.
Upvotes: 3