How to integrate Ehcache with Scala
source link: https://blog.knoldus.com/how-to-integrate-ehcache-with-scala/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to integrate Ehcache with Scala
Reading Time: 2 minutes
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 http://terracotta.org/downloads/bigmemorygo.
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
xxxxxxxxxx
“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
xxxxxxxxxx
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true" monitoring="autodetect"
dynamicConfig="true">
<cache name="testCache"
maxEntriesLocalHeap="1000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
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:
xxxxxxxxxx
var manager: net.sf.ehcache.CacheManager = CacheManager.newInstance("src/main/resources/ehcache.xml")
val cacheConfiguration = new CacheConfiguration("cacheRegionName", 1000)
.eternal(true)
.persistence(newPersistenceConfiguration().strategy(Strategy.LOCALRESTARTABLE))
val cacheRegion = new Cache(cacheConfiguration)
manager.addCache(cacheRegion)
Example :
xxxxxxxxxx
// 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)
.eternal(true)
.persistence(newPersistenceConfiguration().strategy(Strategy.LOCALRESTARTABLE))
// create cache region
val cacheRegion = new Cache(cacheConfiguration)
// add cache region in cacheManager
cacheManager.addCache(cacheRegion)
// 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"
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK