Ehcache 2.0, which was recently released, supports the new Hibernate 3.3/3.5 SPI with its CacheRegionFactory.

This new provider has the following benefits:

  • The SPI removed synchronization in the Hibernate cache plumbing. It is left up to the caching implementation on how to control concurrent access. Ehcache, starting with 1.6, removed syncrhonization in favour of a CAS approach. The results, for heavy workloads are impressive.
  • The new SPI provides finer grained control over cache region storage and cache strategies. Ehcache 2.0 takes advantage of this to reduce memory use. It provides read only, nonstrict read write and read write strategies, all cluster safe.
  • Ehcache 2.0 is readily distributable with Terracotta Server Array. This gives you cluster safe operation (coherency), HA and scale beyond the limits of an in-process cache, which is how most Hibernate users use Ehcache today. There is the existing ehcache.jar and ehcache-terracotta.jar which provides the client library. It is a couple of lines of config to turn that on in ehcache.xml:
<ehcache>
<terracottaConfig url="someserver:9510"/>
<defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToLiveSeconds="600"
            <terracotta clustered="true" coherent="true"/>
/>

    <cache name="com.company.domain.Pets"
           maxElementsInMemory="10000"
           eternal=”true”>
           <terracotta clustered="true" coherent="true"/>
           </cache>
</cache>
</ehcache> 

Enabling Ehcache 2.0 as the CacheRegionFactory

Various versions of Ehcache ship with Hibernate. To get version 2.0 you can either download it or, if you are Maven based, add the following Maven snippet:

       <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>[2.0.0,]</version>
            <type>pom</type>
        </dependency>

You then enable it in your hibernate settings with:

<property name="hibernate.cache.region.factory_class">
        net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
    </property>

More Information

We have been updating the Hibernate Chapter on Ehcache.org. We plant to continue to add tips and tricks together with deep reference information on Hibernate caching with Ehcache there.


Back to top