Hibernate - Memcached

A library for using Memcached as a second level distributed cache in Hibernate.

CircleCI Status

CircleCI

Jump to

Maven dependency

To install it, you just need to add the following Maven dependency (check Versions for the right version for you):

<dependency>
    <groupId>com.github.mihaicostin</groupId>
    <artifactId>hibernate-l2-memcached</artifactId>
    <version>5.4.2.0</version>
</dependency>

https://mvnrepository.com/artifact/com.github.mihaicostin/hibernate-l2-memcached

Example config:

<property name="hibernate.cache.region.factory_class">com.mc.hibernate.memcached.MemcachedRegionFactory</property>
<property name="hibernate.memcached.operationTimeout">5000</property>
<property name="hibernate.memcached.connectionFactory">KetamaConnectionFactory</property>
<property name="hibernate.memcached.hashAlgorithm">FNV1_64_HASH</property>

If memcached authentication is required you can specify username and password:

<property name="hibernate.memcached.username">memcached-username</property>
<property name="hibernate.memcached.password">memcached-password</property>

If memcached are running on a remote server, e.g. on AWS ElastiCache, you can specify the connection URL:

<property name="hibernate.memcached.servers">cache.c3wd5k.cfg.euw1.cache.amazonaws.com:11211</property>

Configuration options

Targeting the memcached client (net.spy.memcached configurations)

Property Default Value
hibernate.memcached.servers localhost:11211
hibernate.memcached.operationQueueLength (default provided by net.spy.memcached.DefaultConnectionFactory)
hibernate.memcached.readBufferSize (default provided by net.spy.memcached.DefaultConnectionFactory)
hibernate.memcached.operationTimeout (default provided by net.spy.memcached.DefaultConnectionFactory)
hibernate.memcached.hashAlgorithm NATIVE_HASH
hibernate.memcached.connectionFactory DefaultConnectionFactory
hibernate.memcached.daemonMode false
hibernate.memcached.username -
hibernate.memcached.password -

Targeting the cache implementation (global or region specific)

Property Default Value
hibernate.memcached.cacheTimeSeconds 300
hibernate.memcached.clearSupported false
hibernate.memcached.memcacheClientFactory com.mc.hibernate.memcached.spymemcached.SpyMemcacheClientFactory
hibernate.memcached.dogpilePrevention false
hibernate.memcached.dogpilePrevention.expirationFactor 2
hibernate.memcached.keyStrategy com.mc.hibernate.memcached.keystrategy.Sha1KeyStrategy

In order to specify a property for a specific region add the region name right after memcached. ex: hibernate.memcached.myregion.cacheTimeSeconds

Versions

Use the version compatible with your hibernate version (ex: Version 5.2.1.x is developed for and compatible with hibernate 5.2.1.Final)

Hibernate 5.4.x

Hibernate 5.3.x

Hibernate 5.2.x

Hibernate 5.1.x

Hibernate 5.0.x

Hibernate 4.3.x

Known issues

Acknowledgements