Ehcache (--ehcache.xml) Introduction

Source: Internet
Author: User
Tags temporary file storage

http://haohaoxuexi.iteye.com/blog/2113728

Ehcache.xml Introduction

The Ehcache.xml file is used to define the configuration information of the Ehcache, and more accurately it is the configuration information that defines the CacheManager. According to our introduction to CacheManager in the article "Ehcache" We know that all Ehcache applications start with CacheManager. When creating CacheManager without specifying a configuration information parameter, CacheManager will first look for a file called Ehcache.xml under the root directory of the classpath as the CacheManager configuration file. If no such file exists, the Ehcahce-failsafe.xml file encapsulated in the Ehcache jar package is used as the default configuration information for creating the CacheManager. In addition to using configuration as a parameter, using other parameters to construct the CacheManager is based on the XML format. When we use the XML configuration file as the configuration information of CacheManager, our file name is not necessarily called ehcache.xml, this is just the Ehcache.xml file as a representative of this kind of file, they have a common document structure. This article will do a brief introduction to Ehcache.xml, the main introduction of common configuration items.

1 Ehcache elements

First of all, our Ehcache.xml file must adhere to the Ehcache XML Schema definition, we can directly use the online http://ehcache.org/ehcache.xsd, or directly from that address to download the Ehcache.xsd file locally. The root element of the Ehcache.xml file must be Ehcache, a ehcache element is equivalent to a cachemanager, the attributes that we specify on the Ehcache element, and the child elements defined under the Ehcache element are for the current CacheManager, such as our A cache element is defined under the Ehcache element, which means that there is such a cache in the CacheManager we define. The attributes on the Ehcache element are optional, and we can specify the location of the schema that the Ehcache.xml file follows on the Ehcache element, such as:

<ehcache xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"     xsi:nonamespaceschemalocation= "http// Ehcache.org/ehcache.xsd ">     

1.1 Optional properties

In addition to specifying the schema that the Ehcache.xml file follows, our Ehcache element can also specify a number of attributes, mainly as follows.

Name: Specifies the names of the current CacheManager.

dynamicconfig: Boolean type. Indicates whether the configuration can be dynamically updated by default to True. When set to false, we will not be able to change the configuration information at run time through CacheManager's config. We can specify this configuration by using the Dynamicconfig (Boolean Dynamicconfig) method of the Configuration object when you configure it with code.

maxbyteslocaldisk: Specifies the maximum capacity of the local disk that can be used at the CacheManager level. When this property is specified, the Overflowtodisk of all caches is implicitly changed to true, and if you want to close, you need to set Overflowtodisk to false on the corresponding cache.

maxbyteslocalheap: Specifies the maximum capacity of the heap memory that can be used at the CacheManager level.

maxbyteslocaloffheap: Specifies the maximum capacity of non-heap memory that can be used at the CacheManager level. When this property is specified, the Overflowtodisk of all caches are implicitly changed to true, and if you want to close, set Overflowtooffheap to False on the corresponding cache. This property is only useful for Enterprise Edition Ehcache.

defaulttransactiontimeoutinseconds:

UpdateCheck: Boolean type, whether to check for updates, default to True. When set to true, CacheManager periodically checks to see if the current Ehcache is the latest version, and if not, it will be listed as newer than the current version.

It is important to note that when we specify maxbyteslocaloffheap at the CacheManager level, the default value of Overflowtooffheap is changed to true. This means that all caches in the CacheManager are true by default when they do not display the specified Overflowtooffheap property value, which is false by default.

2 cache Element

The cache element is a child of the Ehcache element, and a cache element represents the definition of a Ehcache object. The simplest way to define a cache element is simply to specify the name of the defined Ehcache, and the other is to use the default configuration. The default configuration will use the definition of the Defaultcache element (which is described later in this article).

2.1 Properties

There are also many properties that can be specified in the cache element, but only one is required. That's the Name property.

Name: Specify the names of the caches.

maxentrieslocaldisk: Specifies the maximum number of elements allowed on the hard disk, 0 means no limit. This property can also be changed by Cacheconfiguration at runtime.

maxentrieslocalheap: Specifies the maximum number of elements allowed to be stored in memory, 0 means no limit. This property can also be dynamically modified at run time.

maxentriesincache: Specifies the maximum number of elements that are allowed to be stored in the cache. This property can also be dynamically modified at run time. However, this property is only useful for terracotta distributed caches.

maxbyteslocaldisk: Specifies the maximum number of bytes of hard disk that the current cache can use, and the value can be a number plus unit, which can be K, m, or G, not case-sensitive, such as: 30G. When this attribute is specified at the CacheManager level, the cache level can also be expressed as a percentage, such as: 60%, which indicates up to 60% of the capacity of the hard disk specified using the CacheManager level. This property can also be specified at run time. When this property is specified, the overflowtodisk of the current cache is implicitly true.

maxbyteslocalheap: Specifies the maximum number of bytes of heap memory that the current cache can use, and its value is set to the same rules as maxbyteslocaldisk.

maxbyteslocaloffheap: Specifies the maximum number of bytes of non-heap memory that the current cache is allowed to use. When this property is specified, the value of the current cache's overflowtooffheap is changed to true, and if we need to close overflowtooffheap, we need to display the value of the specified Overflowtooffheap as false.

overflowtodisk: Boolean type, default is False. If the cache in memory has reached the preset limit, it is allowed to save elements that are evicted by the eviction policy on the hard disk, the default is LRU (least recently used). When specified as false, the cache information is not saved to disk and is only saved in memory. This property is now deprecated, and it is recommended to replace it with the child element persistence of the cache element, such as: <persistence strategy= "Localtempswap"/>.

diskspoolbuffersizemb: The size of the buffer when writing cache information to disk, in megabytes, by default, 30.

overflowtooffheap: Boolean type, default is False. Indicates whether the cache is allowed to be stored with non-heap memory, and non-heap memory is not affected by Java GC. This property is only useful for Enterprise Edition Ehcache.

copyonread: When you specify this property as true, we fetch a copy of the corresponding element in the cache, rather than a corresponding reference, when we read the data from the cache. The default is False.

copyonwrite: When specifying this property as true, we are writing data to the cache with a copy of the original object instead of a corresponding reference. The default is False.

timetoidleseconds: The unit is the seconds, which indicates the maximum time that an element is allowed to idle, that is, the maximum time an element is allowed to stay in the cache without being requested. The default is 0, which means no limit.

timetoliveseconds: The unit is the second, indicating the maximum time that an element is allowed to exist in the cache, regardless of whether it is idle or not. The default is 0, which means no limit.

Eternal: Boolean type, which indicates whether it is permanent and false by default. If set to true, the elements within Timetoidleseconds and Timetoliveseconds,cache will never expire and will not be purged due to the expiration of the element.

diskexpirythreadintervalseconds : The unit is the second, which indicates how often the thread that checks whether an element expires is run once, the default is 120 seconds.

Clearonflush: Boolean type. Indicates whether to empty the memorystore when the flush method of the cache is called. The default is true.

memorystoreevictionpolicy: The eviction strategy that will be used when the number or size of elements within the memory reaches the specified limit. The default is LRU (least recently used), and the optional values are LFU (most infrequently used) and FIFO (first-in-one-out).

2.2 Child elements

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:

L Localtempswap: When the heap memory or non-heap memory elements are full, the elements are temporarily stored on the disk, once restarted will disappear.

L 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.

L None: Cached elements are not persisted

L 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.

L localmemory: Indicates that the element is pinned in memory.

L Incache: Indicates that the element is pinned to any container in which it is being saved.

3 Defaultcache

The Defaultcache element is used to specify the default configuration of the cache, which can specify information that is roughly the same as the cache element. These configurations only work on caches that are added in the program through the CacheManager Addcache (String cachename) method.

Let's say the contents of our Ehcache.xml file are as follows:

<ehcache xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"     xsi:nonamespaceschemalocation= "http// Ehcache.org/ehcache.xsd "     maxbyteslocaldisk=" 20G "maxbyteslocalheap=" 10M ">            <defaultcache copyOnRead = "true" copyonwrite= "true" overflowtooffheap= "false"/>        <cache name= "Test2" overflowtooffheap= "false"/ >      

We can see that we have specified Defaultcache's copyonread and Copyonwrite as true, indicating that copies of copies are used when reading and fetching. A cache named Test2 is then defined. We then perform the following tests:

@Test  publicvoid Testdefaultcache () {     CacheManager CacheManager = Cachemanager.create ();     CACHE1 will use the default configuration     cachemanager.addcache ("Cache1");     Cache cache1 = Cachemanager.getcache ("Cache1");     Element ele = new Element ("key", "value");     Cache1.put (ele);     Element ele2 = cache1.get ("key");     System.out.println (ele = = ele2);  False         Cache test2 = Cachemanager.getcache ("test2");     Test2.put (ele);     Element ele3 = test2.get ("key");     System.out.println (ele = = Ele3);  True  

We added a cache called Cache1 by CacheManager the name of the buffer, which will use the default configuration, which is defaultcache the specified configuration. Then we define an element ele, and store it in the cache1, because the specified copyonwrite is true, so at the time of storage is actually a copy of the ele, rather than its reference. We then take the ele out of the cache1 and assign it to the element ele2, because we specify Copyonread as true, so we'll take a copy of Ele in the cache. We then compare the Ele and the Ele2, and the result is false. Then we go to the ehcache.xml defined in the cache named Test2 Ele, because Test2 did not specify Copyonwrite is true, so here is the Ele reference. We then read the ele from the Test2 and assign it to ele3, because we did not specify the copyonread of Test2 to be true, so it is still a reference to the ele that was previously stored. They are then compared to their addresses, resulting in equal results.

4 Diskstore elements

The Diskstore element is used to specify the path of the disk storage, which receives only one parameter path, which represents the path to disk storage. Such as:

<diskstore path= "D:\\ehcache"/>  

If you do not want Ehcache to create the path to the disk store, you can not define the Diskstore element. Java.io.tmpdir is used as the disk storage path by default when Diskstore is not defined, but the cache needs to use disk storage.

The Path property of the Diskstore element is automatically replaced with the actual corresponding value when the following value is used.

L Java.io.tmpdir: The default temporary file storage path.

L User.home: The user's home directory.

L User.dir: The user's current working directory, which is the working path of the current program.

L Other System properties specified through the command line, such as "Java–ddiskstore.path=d:\\abc ...".

(Note: This article is written based on Ehcache2.8.1)

Ehcache (--ehcache.xml) Introduction

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.