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...)?

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.

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);
    }

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.