繁体   English   中英

如何在 Hibernate 上使用 Redis 作为 L2 缓存?

[英]How to use Redis as L2 cache on Hibernate?

我有一个 Spring Boot 应用程序,需要在休眠时将 Redis 设置为 l2 缓存。

我的道具文件看起来像:

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

我创建了一个自定义区域工厂,因为我不想使用 json 或 yaml 文件。 (现在,参数是硬编码的)。 CustomRegionFactory 类看起来像:

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

使用 redis-cli 我发现使用命令keys *时列出了所有用@Cacheable注释的实体。 到这里为止,我认为一切正常,但是使用 postgres 日志记录资源我发现查询正在访问数据库。

有人有任何提示可以使其正常工作吗?

您应该使用 .setCachable(true) 使查询在休眠级别缓存。 请参阅文档。

另请参阅有关休眠的二级缓存的问题

我发现在 hibernate 中使用 @Cacheable 可以解决所有问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM