簡體   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