Wednesday, 17 February 2016

Ehcache Configuration

By
Configuring Ehcache into your spring application

<ehcache
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"><diskStore path="ehcache.disk.store.dir"/>  
<cacheManagerEventListenerFactory class="" properties=""/>
Cache Configuration
The following attributes are required.
  • name: Sets the name of the cache. This is used to identify the cache.It must be unique.maxElementsInMemory:Sets the maximum number of objects that will be created in memory
  • maxElementsOnDisk:Sets the maximum number of objects that will be maintained in the DiskStore The default value is zero, meaning unlimited.
  • eternal:Sets whether elements are eternal. If eternal, timeouts are ignored and the   element is never expired.overflowToDisk:Sets whether elements can overflow to disk when the memory store   has reached the maxInMemory limit.
The following attributes and elements are optional.
  • timeToIdleSeconds:Sets the time to idle for an element before it expires.i.e. The maximum amount of time between accesses before an element expires Is only used if the element is not eternal.
  • Optional attribute. A value of 0 means that an Element can idle for infinity.The default value is 0.
  • timeToLiveSeconds:Sets the time to live for an element before it expires.i.e. The maximum time between creation time and when an element expires.
    • Is only used if the element is not eternal.
    • Optional attribute. A value of 0 means that and Element can live for infinity.The default value is 0.
  • diskPersistent:Whether the disk store persists between restarts of the Virtual Machine.The default value is false.
  • diskExpiryThreadIntervalSeconds:The number of seconds between runs of the disk expiry thread. The default value is 120 seconds.
  • diskSpoolBufferSizeMB:This is the size to allocate the DiskStore for a spool buffer. Writes are made to this area and then asynchronously written to disk.
    • The default size is 30MB.
    • Each spool buffer is used only by its cache. If you get OutOfMemory errors consider lowering this value. To improve DiskStore performance consider increasing it.
    • Trace level logging in the DiskStore will show if put back ups are occurring. 
  • memoryStoreEvictionPolicy:Policy would be enforced upon reaching the
    • maxElementsInMemory limit. Default policy is Least Recently Used (specified as LRU).
Other policies available -
First In First Out (specified as FIFO) and Less Frequently Used  (specified as LFU)    Cache elements can also contain sub elements which take the same format of a factory class and properties. Defined sub-elements are:
  • cacheEventListenerFactory - Enables registration of listeners for cache events,such as put, remove,update, and expire.
  • bootstrapCacheLoaderFactory- Specifies a BootstrapCacheLoader, which is called by a cache on initialisation to prepopulate itself.
  • cacheExtensionFactory - Specifies a CacheExtension, a generic mechansim to tie a class which holds a reference to a cache to the cache lifecycle.
  • cacheExceptionHandlerFactory - Specifies a CacheExceptionHandler, which is called when cache exceptions occur.
  • cacheLoaderFactory - Specifies a CacheLoader, which can be used both asynchronously and synchronously to load objects into a cache. More than one cacheLoaderFactory element can be added, in which case the loaders form a chain which are executed in order. If a loader returns null, the next in chain is called.
Mandatory Default Cache configuration.
These settings will be applied to caches created programaticaly using CacheManager .add(String cacheName).The defaultCache has an implicit name "default" which is a
reserved cache name.
<defaultCache maxElementsInMemory="1000" eternal="true"
timeToIdleSeconds="360"        timeToLiveSeconds="360" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="2000" diskPersistent="true" diskExpiryThreadIntervalSeconds="360"                      memoryStoreEvictionPolicy="LRU" />
</ehcache>

0 comments :

Post a Comment