如何将 cache2k 与休眠集成

how to integrate cache2k with hibernate

如何像 hibernate 缓存提供程序一样集成 cache2k?

我的意思是在当前项目中我们使用 ehcache 和 在下一个配置中启用缓存 hibernate.cfg.xml :

<hibernate-configuration>

    <session-factory>

        <!-- Cache Configurations -->
        <!-- Using net.sf.ehcache.hibernate.SingletonEhCacheProvider instead of
             net.sf.ehcache.hibernate.EhCacheProvider ensures the same instance
             of CacheManager is referred to by both Hibernate and our JMX Agent
             simpleJpaHibernateApp.agents.jmxAgent.   -->
        <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</property>
        <!--  <property name="hibernate.cache.provider_configuration">/ehcache.cfg.xml</property> -->
        <property name="hibernate.cache.use_minimal_puts">false</property>
        <property name="hibernate.cache.use_query_cache">true</property>
        <property name="hibernate.cache.use_second_level_cache">true</property>
        <property name="hibernate.cache.use_structured_entries">true</property>
</session-factory>

我们也使用缓存管理器class:

public class AppCacheManager {

private static final Logger log = LoggerFactory.getLogger(AppCacheManager.class);

private static CacheManager manager;

@SuppressWarnings("unused")
private static AppCacheManager INSTANCE = new AppCacheManager(); 


private AppCacheManager() {
    manager = CacheManager.getInstance();

    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    ManagementService.registerMBeans(manager, mBeanServer, true, true, true, true);

    if (log.isInfoEnabled()) {
        log.info("Cache Manager was initialized.");
        log.info("Cache Regions Detected:");

        String[] cacheNames = manager.getCacheNames();
        for (String cacheName : cacheNames) {
            log.info("   " + cacheName);
        }
        log.info("Cache disk store path: " + manager.getDiskStorePath());
    }
}

我可以用一些 cache2k impl 替换休眠配置 net.sf.ehcache.hibernate.SingletonEhCacheProvider 吗?

以及我应该如何采用基于 cache2k 的 AppCacheManager?

谢谢!

您可以将 cache2k 与 hibernate 一起使用,而无需任何额外的代码。

您需要在项目中添加以下依赖:

<dependency>
  <groupId>org.cache2k</groupId>
  <artifactId>cache2k-all</artifactId>
  <version>${cache2k-version}</version>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>org.cache2k</groupId>
  <artifactId>cache2k-jcache</artifactId>
  <version>${cache2k-version}</version>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>javax.cache</groupId>
  <artifactId>cache-api</artifactId>
  <version>1.1.0</version>
</dependency>

hibernate配置需要包含:

<hibernate-configuration>
  <session-factory>
    <property name="cache.region.factory_class">org.hibernate.cache.jcache.JCacheRegionFactory</property>
    <property name="hibernate.javax.cache.provider">org.cache2k.jcache.provider.JCacheProvider</property>
    <property name="hibernate.javax.cache.uri">hibernate</property>
    <!-- .... rest of configuration .... -->
  </session-factory>
</hibernate-configuration>

然后您可以将文件 cache2k-hibernate.xml 添加到允许进一步配置缓存的类路径,例如:

<cache2k>
  <version>1.0</version>
  <!-- if enabled cache2k does not refuse operation in case there is
       no configuration for a requested cache name -->
  <ignoreMissingCacheConfiguration>true</ignoreMissingCacheConfiguration>
  <defaults>
    <!-- default settings for every cache -->
    <cache>
      <entryCapacity>100_000</entryCapacity>
    </cache>
  </defaults>
  <caches>
    <!-- reduced size for the query cache -->
    <cache>
      <name>org.hibernate.cache.internal.StandardQueryCache</name>
      <entryCapacity>100</entryCapacity>
    </cache>
  </caches>
</cache2k>