4

I would like to use Redis as a cache manager in order to cache JPA entities coming from a MySQL database.

I am new to Redis and it seems Redis is only able to cache the basic types/structures it knows (strings, hashes, etc.)

My question is: can I use Redis (together with Spring cache abstraction) as a spring cache manager to cache my custom objects (say a Person, Order, Customer, etc...)?

asked Aug 5, 2013 at 10:13

2 Answers 2

4

You can start by looking at Spring Data Redis, but unlike Spring Data JPA, is doesn't offer repository abstraction, instead using Spring templates with accessor methods specific only to redis. Since Redis does not support relations, you'll have to design and implement these relations by overriding JPA's standard CRUD operations.

Here's a great article that details something up your alley...
http://www.packtpub.com/article/building-applications-spring-data-redis

I am new to Redis and it seems Redis is only able to cache the basic types/structures it knows (strings, hashes, etc.)

Redis can store anything; text, json, binary data, it doesn't matter.

By default, RedisTemplate (part of Spring Data Redis), uses Java serialization to marshal/unmarshall objects to/from redis, but it uses more space in redis compared to something like MessagePack, based on my tests.

answered Aug 5, 2013 at 16:07
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply. Just one last question: can I use the Redis Spring Cache manager with Spring cache abstraction as is or do I need further configuration?
I'm not entirely sure, I'd start with that article as a tutorial, build it out, it will answer many questions for sure.
There's a RedisCacheManager implementation in Spring Data Redis. JavaDocs
3

Redisson offers Redis based Spring Cache provider. It supports such important cache settings like ttl and maxIdleTime for Redis store and supports many popular codecs: Jackson JSON, Avro, Smile, CBOR, MsgPack, Kryo, FST, LZ4, Snappy and JDK Serialization.

Config example is below:

@Configuration
@ComponentScan
@EnableCaching
public static class Application {
 @Bean(destroyMethod="shutdown")
 RedissonClient redisson() {
 Config config = ...
 return Redisson.create(config);
 }
 @Bean
 CacheManager cacheManager(RedissonClient redissonClient) throws IOException {
 Map<String, CacheConfig> config = new HashMap<String, CacheConfig>();
 // ttl = 24 mins, maxIdleTime = 12 mins
 config.put("testCache", new CacheConfig(24*60*1000, 12*60*1000));
 return new RedissonSpringCacheManager(redissonClient, config);
 }
}
answered Feb 10, 2016 at 12:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.