Stand alone program explains how to use Ehcache.
Ehcache is an open-source, standards-based cache for boosting performance, offloading your database, and simplifying scalability. As a robust, proven, and full-featured solution, it is today’s most widely used Java-based cache. You can use Ehcache as a general-purpose cache or a second-level cache for Hibernate.
Dependencies requied to use Ehcache :
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.5.6</version>
</dependency>
A simple Cache xml configuration :
- maxEntriesLocalHeap : Maximum no of elements allowed in memory.
- maxEntriesLocalDisk : Once Heap is full,then element overflow to disk memory.So this property is maximum no of elements allowed in Disk.
- eternal : Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired.
- 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.
- timeToIdleSeconds : Element will expire if idle time reachs to timeToIdleSeconds(in seconds).
- timeToLiveSeconds : Element will expire if life of the element reachs to timeToIdleSeconds(in seconds).
- memoryStoreEvictionPolicy : Policy would be enforced upon reaching the maxEntriesLocalHeap 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)
- transactionalMode : To enable an ehcache as transactions, set the transactionalMode.
- diskStore : If heap memory is full,then elements overflow to disk.Diskstore is the path of disk directory.
For more description of Ehcache xml configuration, please go with ehcache.xml.