Search code examples
javaspringhibernatecache2k

how to integrate cache2k with hibernate


How i can integrate cache2k like hibernate cache provider?

I mean in current project we use ehcache and enable cache in next configuration 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>

As well we use Cache manager 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());
    }
}

Can i replace in hibernate-configuration net.sf.ehcache.hibernate.SingletonEhCacheProvider with some cache2k impl?

And how i should adopt AppCacheManager base on cache2k ?

Thank you!


Solution

  • You can use cache2k with hibernate without any additional code.

    You need to add the following dependencies in your project:

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

    The hibernate configuration needs to contain:

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

    You can then add a file cache2k-hibernate.xml to the classpath which allows further configuration of the caches, for example:

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