Description of parameters used by Ehcache

Source: Internet
Author: User

Description of parameters used by Ehcache


Parameter description in 1.ehcache.xml

Ehcache.xml file:

<?xml version= "1.0"  encoding= "UTF-8"? ><ehcache xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "     xsi:nonamespaceschemalocation=" http://ehcache.org/ehcache.xsd ">       <diskstore path=" Java.io.tmpdir/ehcache "/>         <!--  Default cache  -->    < defaultcache           maxelementsinmemory= " "           eternal= "false"             timetoidleseconds= "        "    timetoliveseconds= "           " Overflowtodisk= "false"/>                <!--  Menu Cache  -->            <cache name= "MenuCache"              maxelementsinmemory= "     "        eternal= "false"             timetoidleseconds= "           " Timetoliveseconds= "           overflowtodisk=" false "            memorystoreevictionpolicy=" LRU "/>"     </ehcache>


Parameter description:

<diskStore>: When the number of objects in the memory cache exceeds maxelementsinmemory, the cache object is written to the disk cache (requires the object to implement the serialization interface).

<diskstore path= "" >: Used to configure the physical path used by the disk cache, the Ehcache disk cache uses a file suffix of *.data and *.index.

Name: The cache name, the unique identifier of the cache (Ehcache will put the cache in HashMap).

maxelementsondisk: The maximum number of elements that can be stored in the disk cache, and 0 for Infinity.

maxelementsinmemory: The maximum number of elements that can be stored in the memory cache, if the element that is placed in the cache exceeds this value, there are two cases.

1) If overflowtodisk=true, the extra elements in the cache will be placed in the disk file.

2) If overflowtodisk=false, replace the elements in the cache based on the Memorystoreevictionpolicy strategy.

Eternal: Whether the object in the cache is permanently valid, that is, whether the memory is permanent, and true ignores Timetoidleseconds and timetoliveseconds.

timetoidleseconds: The allowable idle time (in seconds) of the cached data before it expires, only when Eternal=false is used, the default value is 0 for infinite idle time, which is an optional property that accesses the maximum interval between elements in this cache , if an element in this cache is not accessed at this time, then this element will be purged from the cache.

timetoliveseconds: The allowable lifetime of cached data before expiration (in seconds), only when Eternal=false is used, the default value is 0, which means that the time to live is infinite, that is, an element in the cache is created from a clear time to life. That is, from the time the creation starts, this element is purged from the cache when it is over.

overflowtodisk: When memory is low, whether to enable disk caching (that is, when the number of objects in memory reaches Maxelementsinmemory, Ehcache writes the object to disk), the corresponding property value is found based on the path value in the label. The file written to disk is placed under the path folder, the name of the file is the name of the cache, and the suffix is data.

diskpersistent: If the disk cache is persisted, when the value of this property is true, the system will look for the file named cache name in the disk when initializing, and a file with the suffix named index. This file holds the index of the cache that has been persisted on disk, and when found, it loads the cache into memory, to really persist the cache to disk, and to write the program with the Net.sf.ehcache.Cache.put (element element ) to call the Flush () method.

diskexpirythreadintervalseconds: Disk cache cleanup thread run interval, default is 120 seconds.

diskspoolbuffersizemb: Set buffer size for Diskstore (disk cache), default is 30MB

memorystoreevictionpolicy: Memory storage and release policy, that is, when the maxelementsinmemory limit is reached, Ehcache cleans up memory according to the specified policy, with a total of three strategies, namely LRU (least recently used ), LFU (most commonly used), FIFO (first-in-one-out).



Description of parameters in 2.service layer usage

2.1 Adding a cache

@Cacheable (value= "Menucache", key= "' Usermenukey ' + #userid") public list<menubean> Querymenubyuserid (String UserID) {...}


The @Cacheable is primarily for method configurations and is capable of caching results based on the request parameters of the method.


@CachePut mainly for the method configuration, can be based on the method's request parameters to cache its results, and @Cacheable is different, it will trigger the real method of invocation each time.


2.2 Clearing the cache

@CacheEvict (value= "Menucache", key= "' Usermenukey ' + #userRoleBean. GetUserId ()") Public boolean deluserrole ( Userrolebean Userrolebean) {...}

The @CachEvict is primarily for method configurations, allowing the cache to be emptied according to certain conditions.


3.ehcache storing cached content on the hard disk

<diskstore path= "C:/temp"/>

<cache name= "AuthCache1"
maxelementsinmemory= "36"
Maxelementsondisk= "10000"
Overflowtodisk= "true"

Diskpersistent= "true"
diskspoolbuffersizemb= "20"
/>

When the amount of data in memory exceeds the maximum number of records set maxelementsinmemory, the excess number of records will be saved to the hard disk, but the contents already in memory will not be automatically saved to the hard disk, you need to manually use Cache.flush () to force the memory contents into the hard disk;

The value of general Maxelementsondisk needs to be set larger than maxelementsinmemory, so the efficiency will be higher;

You need to set Overflowtodisk to true to indicate the use of a hard disk cache policy;

You need to set Diskpersistent to True, otherwise the cache file will be cleaned up after restarting the service, and the property will slow down the cache to some extent, because it needs to constantly monitor whether the size of the hard disk being set is full with the associated property of Diskspoolbuffersizemb. If the hard disk is large enough, you can set the property to large enough;

Finally, *.index and *.data2 files are generated;


* Rebuilding the cache automatically reads data from the hard disk into memory


Description of parameters used by Ehcache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.