MyBatis Ehcache Adapter - Reference Documentation

Ehcache is a widely used java distributed cache for general purpose caching, Java EE and light-weight containers.

The Ehcache integration is built on top of the ehcache-core and comes without any Ehcache 3rd party applications. Please refer to official Ehcache documentation if you need plugins.

To use Ehcache in your application download the zip bundle, decompress it and add the jars to the classpath.

If you are using Maven then simply add to your pom.xml the following dependency:

<dependencies>
  ...
  <dependency>
    <groupId>org.mybatis.caches</groupId>
    <artifactId>mybatis-ehcache</artifactId>
    <version>1.1.0</version>
  </dependency>
  ...
</dependencies>

then, just configure a cache element in your mapper XML files as follows:

<mapper namespace="org.acme.FooMapper">
  <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
  ...
</mapper>

You can also provide values for parameters that can be modified dynamically at runtime:

<mapper namespace="org.acme.FooMapper">
  <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
    <property name="timeToIdleSeconds" value="3600"/><!--1 hour-->
    <property name="timeToLiveSeconds" value="3600"/><!--1 hour-->
    <property name="maxEntriesLocalHeap" value="1000"/>
    <property name="maxEntriesLocalDisk" value="10000000"/>
    <property name="memoryStoreEvictionPolicy" value="LRU"/>
  </cache>
  ...
</mapper>

You may need to use a blocking cache. See the details here. This is how it is configured:

<mapper namespace="org.acme.FooMapper">
  <cache type="org.mybatis.caches.ehcache.EhBlockingCache"/>
  ...
</mapper>

Users that need to configure Ehcache through XML configuration file, have to put in the classpath the /ehcache.xml resource. Please refer to the official Ehcache documentation to know more details.

If the /ehcache.xml resource is not found or something goes wrong while loading it, the default configuration will be used.