<?xml version= "1.0" encoding= "UTF-8"?>
<ehcache>
<!--Timetoidleseconds when the cache is idle for n seconds and then destroyed-
<!--Timetoliveseconds when the cache is alive N seconds and then destroyed-
<!--
Cache configuration
Name: Cache names.
Maxelementsinmemory: Maximum number of caches.
Eternal: The object is permanently valid, but if set, timeout will not work.
Timetoidleseconds: Sets the allowable idle time (in seconds) for an object before it expires. An optional property is used only if the Eternal=false object is not permanently valid, and the default value is 0, which means that the idle time is infinite.
Timetoliveseconds: Sets the time (in seconds) that an object is allowed to survive before it expires. The maximum time is between the creation time and the expiration time. Used only when the Eternal=false object is not permanently valid, the default is 0, which means that the object survives indefinitely.
Overflowtodisk: When the number of objects in memory reaches Maxelementsinmemory, Ehcache writes the object to disk.
DISKSPOOLBUFFERSIZEMB: This parameter sets the buffer size of the Diskstore (disk cache). The default is 30MB. Each cache should have its own buffer.
Maxelementsondisk: Maximum number of hard disk caches.
Diskpersistent: Whether to cache VM Restart period data Whether The disk store persists between restarts of the virtual machine. The default value is False.
Diskexpirythreadintervalseconds: Disk failed thread run time interval, default is 120 seconds.
Memorystoreevictionpolicy: When the maxelementsinmemory limit is reached, Ehcache will clean up the memory according to the specified policy. The default policy is LRU (least recently used). You can set it to FIFO (first in, out) or LFU (less used).
Clearonflush: If the maximum amount of memory is cleared.
Statistics: Whether statistical information is collected. If you need to monitor cache usage, you should turn this option on. The default is off (statistics can affect performance). Set statistics= "True" to turn on statistics.
Copyonread: Returns a copy of a cached item when it is read out (the returned object is a copy of the object in the cache). False by default
Copyonwrite: When a cache entry is written, writes a copy of it (writes to the cached copy of the object). False by default.
-
Java code
<DiskstorePath= "Java.io.tmpdir" /> <Defaultcachemaxelementsinmemory= "$"Eternal= "false"Timetoidleseconds= "+"Timetoliveseconds= "$"Overflowtodisk= "true" /> <Cachename= "com. Menu "maxelementsinmemory= "Max"Eternal= "false"Timetoliveseconds= "36000"Timetoidleseconds= "3600"Overflowtodisk= "true"/> </Ehcache>
<!--persistence: child element settings
Persistence: Represents the persistence of the cache, which has only one attribute strategy, which represents the persistence policy corresponding to the current cache. The optional values are as follows:
Localtempswap: When the heap memory or non-heap memory elements are full, the elements are temporarily stored on the disk, once the restart will disappear.
Localrestartable: This policy is only useful for Enterprise Edition Ehcache. It can persist the contents of the heap or non-heap memory to the hard disk at reboot, then restore the elements from the hard disk to the memory after rebooting.
None: Cached elements are not persisted
Distributed: This policy does not apply to standalone machines and is for distribution.
Copystrategy: When we specify Copyonread or Copyonwrite as true, we use our copystrategy, which is the copy policy. The default copystrategy is implemented by serialization, We can implement our own copystrategy by implementing the Net.sf.ehcache.store.compound.CopyStrategy interface, and then simply define a COPYSTRATEGY element under the cache element and specify its Class property as our copy The Strategy implementation class. such as: <copystrategy class= "Xxx.xxx.xxx"/>.
Pinning: Indicates that the elements within the cache are pinned and will not be deleted or evicted into other storage containers unless they expire. The pinning element defines only one property store, which indicates where the element will be pinned. The optional values are localmemory and Incache.
Localmemory: Indicates that the element is pinned in memory.
Incache: Indicates that the element is pinned to any container in which it is being saved.
<?XML version= "1.0" encoding= "UTF-8"?><EhcacheXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:nonamespaceschemalocation= "Http://ehcache.org/ehcache.xsd"> <!-- Cache configuration. The following attributes is required. Name:sets the name of the cache. This was used to identify the cache. It must be unique. Maxelementsinmemory:sets the maximum number of objects that would be a created in memory (0 = = No limit) Maxelementson Disk:sets the maximum number of objects that would be a maintained in the Diskstore the default value is zero, meaning Unlimited. Eternal:sets whether elements is eternal. If Eternal, timeouts is ignored and the element is never expired. Overflowtodisk:sets whether elements can overflow to disk when the In-memory cache have reached the Maxinmemory Limi T. The following attributes is 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 are only used if the element was not eternal. Optional attribute. A value of 0 means that A Element can idle for infinity. THe default value is 0. Timetoliveseconds:sets the time to live for a element before it expires. i.e. the maximum time between creation time and when an element expires. Was only used if the element was 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 seconds. Diskspoolbuffersizemb:this is the size of the allocate the Diskstore for a spool buffer. Writes is made to the and then asynchronously written to disk. The default size is 30MB. Each of the 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 is occurring. Memorystoreevictionpolicy:policy would be enforced upon reaching the maxelementsinmemory limit. The Default policy is Least recently used (specified as LRU). Other policies Available-first on first out (specified as FIFO) and less frequently used (specified as LFU) Cac He elements can also contain sub elements which take the same format of a factory class and properties. Defined sub-elements is: * cacheeventlistenerfactory-enables registration of listeners for cache events, such as Put, remove, update, and expire. * Bootstrapcacheloaderfactory-specifies a Bootstrapcacheloader, which is called-a cache on initialisation to pre Populate itself. Each cache that would be distributed needs to set a cache event listener which replicates messages to the other Cacheman Ager peers. For the built-in RMI implementation-is-done by adding a cacheeventlistenerfactory element of type Rmicachereplicat Orfactory To each distributed cache ' s configuration as per the following example: <cacheeventlistenerfactory class= "net.sf . Ehcache.distribution.RMICacheReplicatorFactory "properties=" Replicateasynchronously=true, replicateputs=t Rue, Replicateupdates=true, Replicateupdatesviacopy=true, replicateremovals=true "/> The RMI Cachereplicatorfactory recognises the following properties: * Replicateputs=true|false-whether new elements placed in A cache is replicated to others. Defaults to True. * Replicateupdates=true|false-whether new elements which override an element already existing with the same key is Replicated. Defaults to True. * Replicateremovals=true-whether element removals is replicated. Defaults to True. * replicateasynchronously=true | False-whether replications is asynchronous (true) or synchronous (false). Defaults to True. * replicateupdatesviacopy=true | False-whether the new elements is Copied to other caches (true), or whether a remove message is sent. Defaults to True. * Asynchronousreplicationintervalmillis=<number of Milliseconds>-the asynchronous replicator runs at a set in Terval of milliseconds. The default is 1000. The minimum is 10. This property was only applicable if replicateasynchronously=true * asynchronousreplicationmaximumbatchsize=<number o F operations>-The maximum number of operations that would be is batch within a single RMI message. The default is 1000. Applicable if replicateasynchronously=true the Rmibootstrapcacheloader bootstraps caches in Clust ERS where rmicachereplicators is used. IT is configured as per the following example: <bootstrapcacheloaderfactory class= "Net.sf.ehcache.distributio N.rmibootstrapcacheloaderfactory "properties=" bootstrapasynchronously=true, maximumchunksizebytes=5000000 "/> The Rmibootstrapcacheloaderfactory recognises the FOLlowing Optional Properties: * Bootstrapasynchronously=true|false-whether The bootstrap happens in the background After the cache has started. If false, bootstrapping must complete before the cache is made available. The default value is true. * maximumchunksizebytes=<integer>-Caches can potentially be very large, larger than the memory limits of the VM. This property allows the bootstraper to fetched elements in chunks. The default chunk size is 5000000 (5MB). - <DiskstorePath= "Java.io.tmpdir"/> <!--Mandatory Default Cache configuration. These settings'll is applied to caches created programmtically using Cachemanager.add (String cachename) - <Defaultcachemaxelementsinmemory= "10000"Eternal= "false"Timetoidleseconds= "+"Timetoliveseconds= "+"Maxelementsondisk= "10000000"Diskexpirythreadintervalseconds= "+"Memorystoreevictionpolicy= "LRU"> <PersistenceStrategy= "Localtempswap"/> </Defaultcache> <Cachename= "Cn.com.sina.dlfreebird.blog.cache"maxelementsinmemory= "+"Maxelementsondisk= "+"Overflowtodisk= "true"Eternal= "true"> </Cache> </Ehcache>
Ehcacheutil Tool class use:
Java code
Public classEhcacheutil {Private Static FinalString Path = "/ehcache.xml"; PrivateURL url; PrivateCacheManager Manager; Private StaticEhcacheutil EhCache; Privateehcacheutil (String path) {URL=getclass (). getresource (path); Manager=cachemanager.create (URL); } Public Staticehcacheutil getinstance () {if(ehcache==NULL) {EhCache=Newehcacheutil (path); } returnEhCache; } Public voidput (string cachename, String key, Object value) {Cache cache=Manager.getcache (CacheName); Element element=NewElement (key, value); Cache.put (Element); } PublicObject Get (String cachename, string key) {Cache cache=Manager.getcache (CacheName); Element element=Cache.get (key); returnelement = =NULL?NULL: Element.getobjectvalue (); } PublicCache Get (String cachename) {returnManager.getcache (CacheName); } Public voidRemove (string cachename, string key) {Cache cache=Manager.getcache (CacheName); Cache.remove (key); } }
Ehcache configuration detailed and CacheManager use