MyBatis Memcached integration - Reference Documentation

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering..

The Memcached integration is built on top of the spymemcached client, written by Dustin Sallings.

Users that want to use Memcached into their applications, have to download the zip bundle, decompress it and add the jars in the classpath; Apache Maven users instead can simply add in the pom.xml the following dependency:

<dependencies>
  ...
  <dependency>
    <groupId>org.mybatis.caches</groupId>
    <artifactId>mybatis-memcached</artifactId>
    <version>1.1.2-SNAPSHOT</version>
  </dependency>
  ...
</dependencies>

then, just configure it in the mapper XML

<mapper namespace="org.acme.FooMapper">
  <cache type="org.mybatis.caches.memcached.MemcachedCache" />
  ...
</mapper>

The Memcached cache is configurable by putting the /memcached.properties classpath resource; if not found, the client will use the default setting.

The following table resumes the supported configurations params; each parameter is optional, if not found in the configuration, the client will use the default value:

Property Default Description
org.mybatis.caches.memcached.keyprefix _mybatis_ any string identifier
org.mybatis.caches.memcached.servers localhost:11211 space separated list of ${host}:${port}
org.mybatis.caches.memcached.connectionfactory net.spy.memcached.DefaultConnectionFactory Any class that implements net.spy.memcached.ConnectionFactory
org.mybatis.caches.memcached.expiration the number of seconds in 30 days the expiration time (in seconds)
org.mybatis.caches.memcached.asyncget false flag to enable/disable the async get
org.mybatis.caches.memcached.timeout 5 the timeout when using async get
org.mybatis.caches.memcached.timeoutunit java.util.concurrent.TimeUnit.SECONDS the timeout unit when using async get
org.mybatis.caches.memcached.compression false if true, objects will be GZIP compressed before putting them to Memcached

If users need to log cache operations, they can plug the Cache logging version:

<mapper namespace="org.acme.FooMapper">
  <cache type="org.mybatis.caches.memcached.LoggingMemcachedCache" />
  ...
</mapper>