简体   繁体   中英

Hibernate 2nd level cache without ehcache.xml

I am trying to configure Ehcache as a hibernate 2nd-level cache, and so far all the examples I've found instruct you to create an ehcache.xml file in the classpath like:

<ehcache updateCheck="false">

    <diskStore path="java.io.tmpdir" />

    <defaultCache maxElementsInMemory="10000" eternal="false"
        statistics="true" timeToIdleSeconds="120" timeToLiveSeconds="120"
        overflowToDisk="true" diskPersistent="false" 
        diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />

    <cache name="com.yourcompany.CachedEntity" eternal="true" maxElementsInMemory="1000" />     

</ehcache>

And then configure Hibernate as follows:

<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
<property name="net.sf.ehcache.configurationResourceName" value="/ehcache.xml" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.use_second_level_cache" value="true" />

In my workplace we are encouraged to use java config wherever possible and avoid XML config files. I'd appreciate any pointers on how this can be achieved.

The stackoverflow question using ehcache in spring 4 without xml mentioned by learningJava shows how to configure an ehcache CacheManager in java but you still need a way to tell hibernate that it should use your java configured CacheManager .

A possible way to this would be to configure a custom ehcache region factory via the hibernate.cache.region.factory_class property. If you look at the implementation of SingletonEhCacheRegionFactory you'll see that it will be quite easy to swap in your own CacheManager .

I suspect that the mapping between the java configuration and the xml configuration of a cache is quite straightforward, but if ehcache performs some 'magic stuff' behind the scenes when it parses the xml configuration, the job of getting your java configuration right might be a bit trickier.

I also could not find sample code for this problem so I thought that I would share my solution. @Pieter 's answer set me on the right track and you need to implement your own EhCacheRegionFactory class so that you can provide custom java Configuration objects. It appears that the stock library only supports xml. Here's what my solution looks like:

public class CustomEhCacheRegionFactory extends EhCacheRegionFactory{

  private static final EhCacheMessageLogger LOG = Logger.getMessageLogger(
        EhCacheMessageLogger.class,
        CustomCacheRegionFactory.class.getName()
  );

  @Override
  public void start(Settings settings, Properties properties) throws CacheException {
    this.settings = settings;
    if ( manager != null ) {
        LOG.attemptToRestartAlreadyStartedEhCacheProvider();
        return;
    }

    try {

        final Configuration configuration = new Configuration();
        configuration.addCache( getEntityCacheConfiguration() );

        manager = new CacheManager( configuration );
        mbeanRegistrationHelper.registerMBean( manager, properties );
    }
    catch (net.sf.ehcache.CacheException e) {
        //handle
    }
  }

  /**
   * Create the basic second level cache configuration
   * @return
   */
  private CacheConfiguration getEntityCacheConfiguration()
  {
    CacheConfiguration config = new CacheConfiguration("entity", 50000);
    config.setTimeToIdleSeconds(86400);
    config.setTimeToLiveSeconds(86400);

    return config;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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