Skip to content
Advertisement

How to use Redis as L2 cache on Hibernate?

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?

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement