【问题标题】:Is it possible to use Cache2k as hibernate second level cache and also as a spring cache manager?是否可以将 Cache2k 用作休眠二级缓存以及弹簧缓存管理器?
【发布时间】:2018-12-26 13:53:09
【问题描述】:

我正在使用基于 Spring 的应用程序(不是 Spring Boot),我正在尝试将 cache2k 作为 Spring 缓存管理器引入。

目前cache2k通过设置以下属性被用作Hibernate二级缓存

hibernate.cache.use_second_level_cache = true
hibernate.cache.region.factory_class = org.hibernate.cache.jcache.JCacheRegionFactory
hibernate.javax.cache.provider = org.cache2k.jcache.provider.JCacheProvider

带有如下cache2k.xml配置文件(仅与示例相关的部分)

<cache2k xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
     xmlns='https://cache2k.org/schema/v1.x'
     xsi:schemaLocation="https://cache2k.org/schema/v1.x https://cache2k.org/schema/cache2k-core-v1.x.xsd">
<!-- The configuration example for the documentation -->
<version>1.0</version>

<skipCheckOnStartup>true</skipCheckOnStartup>
<ignoreMissingCacheConfiguration>true</ignoreMissingCacheConfiguration>

<defaults>
    <cache>
        <entryCapacity>100</entryCapacity>
    </cache>
</defaults>

<templates>
    <cache>
        <name>lookupTable</name>
        <expireAfterWrite>1d</expireAfterWrite>
        <entryCapacity>50_000</entryCapacity>
    </cache>
</templates>

<caches>

    <cache>
        <name>AdvertisingCategoryCache</name>
        <include>lookupTable</include>
    </cache>

</caches>

现在我想介绍一个 Spring 缓存管理器,并且在 cache2k 文档中有足够的信息来介绍它作为一个 Spring 缓存管理器,也可以使用以下代码

@EnableCaching
...
...

@Bean
public CacheManager cacheManager() {
    return new SpringCache2kCacheManager();
}

最终目标是在需要但调用以下方法时,也在spring缓存层中重用现有的hibernate二级缓存

@Cacheable(cacheNames = "AdvertisingCategoryCache")
@Override
public AdvertisingCategory findById(Long id) {
    // call to the repository
}

我收到以下错误

Cache already created: 'AdvertisingCategoryCache'

关键是我知道缓存已经创建。我的需要是重用现有的。

我该怎么做?

附:这个例子很简单,当然我可以直接删除@Cacheable 方法,但我使用它是为了简单解释我的情况。

【问题讨论】:

    标签: caching spring-cache cache2k


    【解决方案1】:

    编辑:由于您没有使用 Spring Boot,因此您需要确保设置将缓存管理器与 Hibernate 链接的属性。

    首先您需要使用 JCache,因此您必须替换自定义缓存配置才能使用 JCache(Hibernate 不提供 cache2k 的缓存抽象实现)。

    您应该有某种自定义 JPA 设置来配置 EntityManagerFactory。在该设置中,确保注入您创建的CacheManager。如果您有注入点,上下文将确保在尝试配置 Hibernate 之前完全解析 CacheManager(这绝对是您想要的)。

    完成后,添加以下 Hibernate 属性:

    hibernateProperties.put(ConfigSettings.CACHE_MANAGER, cacheManager.getCacheManager());
    

    其中cacheManagerJCacheCacheManager(包装原生javax.cache.CacheManager 的Spring 实现)。

    如果您使用 Spring Boot,事情会变得更容易。首先,您应该确保让 Spring Boot 配置缓存管理器,而不是自己做。由于您使用的是 JCache for Hibernate,因此只需在 Spring Boot 中使用相同的机制,因为它是完全透明的(我们将在幕后将 JCache CacheManager 包装在正确的抽象中)。

    如果你这样做,Spring Boot 将确保在休眠启动之前启动 JCache 缓存管理器。还有一个专门的方法来解释how to configure the integration between the two

    完成此操作后,将共享同一个缓存管理器。

    【讨论】:

    • 您好 Stephane,感谢您的回答。这正是我需要的,但您的回答是指 Spring Boot 应用程序,但如前所述,不幸的是我的不是 Spring Boot 应用程序。
    • 对不起,我错过了。然后您需要在您的自定义 JPA 设置中执行我已经指出的操作。确保特别删除那里的最后两个属性,以便 Hibernate 不会尝试自行查找缓存基础结构。我用非 Spring Boot 建议编辑了我的答案
    猜你喜欢
    • 2011-10-26
    • 2010-10-20
    • 1970-01-01
    • 2023-03-03
    • 2016-06-23
    • 1970-01-01
    • 2017-07-05
    • 2011-07-08
    相关资源
    最近更新 更多