skip to Main Content

I have a spring boot application and need to setup Redis as l2 cache on hibernate.

My prop file looks like:

spring.jpa.properties.hibernate.cache.region.factory_class = package.CustomRegionFactory
spring.jpa.properties.hibernate.cache.redisson.fallback=false

I created a custom region factory because I don’t want to use json or yaml files. (right now, the parameters are hardcoded).
CustomRegionFactory class looks like:

public class CustomRegionFactory extends RedissonRegionFactory {

    @Override
    public RedissonClient createRedissonClient(Properties properties) {     
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379").setRetryInterval(1500)
                .setRetryAttempts(3).setConnectTimeout(10000)
                .setClientName("client1");

        return Redisson.create(config);
    }
}

Using redis-cli I found out that all my entities annotated with @Cacheable are listed when using the command keys *. Until here I thought everything worked fine, but using the postgres logging resources I found out that the queries are hitting the database.

Does somebody have any tips to make it works?

2

Answers


  1. Chosen as BEST ANSWER

    I found out that using @Cacheable from hibernate will solve everything.


  2. You should use .setCachable(true) to make queries to be cached on hibernate level.
    See this documentaion.

    Also see this question regarding second level cache on hibernate

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search