Search code examples
javaspringredisjacksonspring-cache

Is it possible to customize serialization used by the Spring Cache abstraction?


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.


Solution

  • 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.