How to integrate Ehcache with Scala

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

Ehcache was originally developed by Greg Luck, starting in 2003.In 2009, the project was purchased by Terracotta, who provides paid support.

For integrating Ehcache in scala steps are :

1) First we need to Download BigMemory GO from

2) Once it is downloaded we will get terracotta-license.key on our email which needs to be put in resource folder.

3) Put ehcache.xml in resource folder.

4) Add related jars in classpath .

a) ehcache-ee-2.7.5.jar
b) bigmemory-4.0.5.jar

Or add following library dependencies in build.sbt

“org.terracotta.bigmemory" % "bigmemory" % "4.0.5"
"net.sf.ehcache" % "ehcache-ee" % "2.7.5"

Above steps will give you the access of configuring ehcache in scala.

Ehcache configuration : We can configure ehcache in two ways.

1) Declarative : We have to configure our cache region in ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
 <ehcache xmlns:xsi=""
 updateCheck="true" monitoring="autodetect"

 <cache name="testCache"
 <persistence strategy="localTempSwap"/>


Here maxEntriesLocalHeap are the total number of entries which a cache region can have.
Its default value is 0 which means our cache region can have any number of entries.

eternal and (timeToIdleSeconds , timeToLiveSeconds ) are vice versa means if eternal = true then there is no meaning of  timeToIdleSeconds and timeToLiveSeconds . Elements in cache region will not expire.

localTempSwap is a persistence strategy which write the data in disk but when you restart the system , this cache related data will automatically removes from the disk.

2) Programatically:

var manager: net.sf.ehcache.CacheManager = CacheManager.newInstance("src/main/resources/ehcache.xml")

 val cacheConfiguration = new CacheConfiguration("cacheRegionName", 1000)

 val cacheRegion = new Cache(cacheConfiguration)

Example :

// create CacheManager
var cacheManager: net.sf.ehcache.CacheManager = CacheManager.newInstance("src/main/resources/ehcache.xml")

//create the configuration of the cache . Here "testCache" is the name of the cache region
 val cacheConfiguration = new CacheConfiguration("testCache", 1000)

 // create cache region
 val cacheRegion = new Cache(cacheConfiguration)

 // add cache region in cacheManager

 // get the cache region by the name "testCache"
 val testCacheRegion = cacheManager.getCache("testCache")

 // insert the value in cache
 testCacheRegion.put(new Element("key", "testCacheValue"))

 // get the value from the cache region
 val getValue = testCacheRegion.get("key").getValue()
 println(" value from the cache :" , getValue)

 // You will get "testCachevalue"

This entry was posted in Scala and tagged , . Bookmark the permalink.

2 Responses to How to integrate Ehcache with Scala

  1. vinaycn says:

    Nice article.
    You can also wrap EhCache such that one can perform classic “scala” style operations more intuitively as well
    Here is one such attempt I took

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s