Ehcache Introduction Classic: The second ehcache.xml parameter

Source: Internet
Author: User
Tags time interval

Continue with the first article

Diskstore
Path: Specifies the path to the storage object on the hard disk
The directory where the Path property can be configured is:
User.home (user's home directory)
User.dir (user's current working directory)
Java.io.tmpdir (the default temp directory)
Ehcache.disk.store.dir (configuration directory for Ehcache)
Absolute path (for example: C:\\ehcache)

   <diskstore path= "G:\\eclipse\\workspace8\\ehcache\\src\\com\\ij34\\cache"/>
     System.out.println ("Userhome:" +system.getproperty ("User.home"));
System.out.println ("Userdir:" +system.getproperty ("User.dir")); System.out.println ("TmpDir:" +system.getproperty ("Java.io.tmpdir"));

The following attributes are required:
1, Name:cache name, must be unique (Ehcache will put this cache in HashMap).
2. Maxelementsinmemory: The maximum number of element that is cached in memory.
3. Maxelementsondisk: The maximum number of element that is cached on disk, the default value is 0, which means no limit.
4. Eternal: Sets whether the cached elements will never expire. If true, the cached data is always valid, and if False then it is judged according to Timetoidleseconds,timetoliveseconds.
5, Overflowtodisk: If the in-memory data exceeds the memory limit, whether to cache to disk.


second, the following properties are optional:
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.
1, Diskpersistent: whether to persist on disk. Indicates whether the data is valid after restarting the JVM. The default is False.
2.Memorystoreevictionpolicy: If the in-memory data exceeds the memory limit, the policy is cached to the disk. Default LRU, optional FIFO, LFU.
3, 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.
4.Maxentrieslocalheap: Specifies the maximum number of elements that are allowed to be stored in memory, and 0 indicates no limit. This property can also be dynamically modified at run time.
5. 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.
6.Maxbyteslocaldisk: Specifies the maximum number of bytes of a hard disk that the current cache can use, and the value can be a number plus unit, either K, M, or G, which is 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.
7. 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.
8.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.
9, 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"/>.
10.Diskspoolbuffersizemb: The size of the buffer when writing cache information to disk, in megabytes, by default, 30.
11, 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.
12.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.
13. Copyonwrite: When you specify this property as true, we use a copy of the original object instead of a reference to write the data to the cache. The default is False.
14.Timetoidleseconds: Units are 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.
15, Timetoliveseconds: Unit is the second, indicating whether an element is idle or not, it allows the maximum time to exist in the cache. The default is 0, which means no limit.
16.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.
17, Diskexpirythreadintervalseconds: Unit is the second, indicating how long to check whether the element expires thread how often run once, the default is 120 seconds.
18.Clearonflush: Boolean type. Indicates whether to empty the memorystore when the flush method of the cache is called. The default is true.


Three, child elements
3.1.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.
3.2,copystrategy: When we specify the Copyonread or Copyonwrite is true, we will use our copystrategy, that is, the copy strategy. 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"/>.
3.3,pinning: The element in the cache is fixed, unless it expires, it will not be deleted and evicted into other storage containers. 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.

Four, 3 kinds of cache emptying policy:
1, FIFO, first Out (LIFO).
2, LFU, less frequently Used (minimum use). It means that it has been used at least for a long time. The cached element has a hit attribute, and the least hits value is cleared out of the cache.
3, LRU, Least recently used (least recently used). (ehcache default). The cached element has a timestamp, and when the cache is full and you need to make room to cache the new element, the element with the time stamp in the existing cache element that is farthest from the current time is cleared out of the cache.

Wu, Defaultcache
Defaultcache:
Default cache configuration information, if not specified, all objects are processed according to this configuration item
Maxelementsinmemory: Sets the upper limit of the cache and stores the maximum number of record objects
Eternal: Indicates whether the object never expires
Timetoidleseconds: The biggest daze time
Timetoliveseconds: Maximum Time to live
Overflowtodisk: Whether an object is allowed to be written to disk

Vi. methods

ways to create CacheManager :

Method One:
CacheManager manager = new CacheManager ();

Method Two:
CacheManager manager = new CacheManager ("Src/config/ehcache.xml");

Method Three:
URL url = getclass (). GetResource ("/anotherconfigurationname.xml");
CacheManager manager = new CacheManager (URL);

Method Four:
InputStream fis = new FileInputStream (New File ("Src/config/ehcache.xml"). GetAbsolutePath ());
try {
CacheManager manager = new CacheManager (FIS);
} finally {
Fis.close ();
}


get cachenames list :

Method One:
Cachemanager.create ();
string[] Cachenames = Cachemanager.getinstance (). Getcachenames ();

Method Two:
CacheManager manager = new CacheManager ();
string[] Cachenames = Manager.getcachenames ();

Method Three:
CacheManager manager1 = new CacheManager ("Src/config/ehcache1.xml");
CacheManager manager2 = new CacheManager ("Src/config/ehcache2.xml");
string[] CacheNamesForManager1 = Manager1.getcachenames ();
string[] CacheNamesForManager2 = Manager2.getcachenames ();

To add and remove cache elements :

Set a new cache named Testcache, with the default property:
CacheManager Singletonmanager = Cachemanager.create ();
Singletonmanager.addcache ("Testcache");
Cache test = Singletonmanager.getcache ("Testcache");

Set a new cache named Testcache, and define its properties:
CacheManager Singletonmanager = Cachemanager.create ();
Cache Memoryonlycache = new cache ("Testcache", 5, False, False, 2);
Singletonmanager.addcache (Memoryonlycache);
Cache test = Singletonmanager.getcache ("Testcache");

The Cache Property Description:

constructor Function :
Public Cache (String name,

int Maxelementsinmemory,

Boolean Overflowtodisk,
Boolean Eternal,

Long Timetoliveseconds,

Long Timetoidleseconds)

Parameter description:
Name: element name.
Maxelementsinmemory: Sets the maximum value of the object created in memory.
Overflowtodisk: Sets whether the element can be written to disk when the in-memory cache reaches the maxinmemory limit.
Eternal: Sets whether elements reside permanently.
Timetoidleseconds: Sets the pause time before an element dies. That is, the maximum time interval value of two access time before an element dies. Only valid if the element is not permanently resident.
Timetoliveseconds: Sets the time to live before an element dies. That is, the maximum time interval value from build to extinction for an element. Only valid if the element is not permanently resident.

To Delete a cache element :


CacheManager Singletonmanager = Cachemanager.create ();
Singletonmanager.removecache ("Testcache");


Close the cache manager CacheManager

Cachemanager.getinstance (). Shutdown ();


Actions for cached objects:


put a simple object into the cache element ;
Cache cache = Manager.getcache ("Testcache");
element element = new Element ("Key1", "value1");
Cache.put (Element);

get a Serialized object property value ;
Cache cache = Manager.getcache ("Testcache");
Element element = Cache.get ("Key1");
Serializable value = Element.getvalue ();

get an object property value that is not serialized ;
Cache cache = Manager.getcache ("Testcache");
Element element = Cache.get ("Key1");
Object value = Element.getobjectvalue ();

delete an object from an element ;
Cache cache = Manager.getcache ("Testcache");
element element = new Element ("Key1", "value1");
Cache.remove ("Key1");

For permanent disk storage, save to disk immediately:

Cache cache = Manager.getcache ("Testcache");
Cache.flush ();

Get Cache Size:

the number of objects that are cached ;
Cache cache = Manager.getcache ("Testcache");
int elementsinmemory = Cache.getsize ();

Get the amount of memory the cache object consumes
Cache cache = Manager.getcache ("Testcache");
Long elementsinmemory = Cache.getmemorystoresize ();

Gets the amount of disk that the cache consumes to the object
Cache cache = Manager.getcache ("Testcache");
Long elementsinmemory = Cache.getdiskstoresize ();

About cached Read and lost records:

Gets the number of hits that the cache reads;
Cache cache = Manager.getcache ("Testcache");
int hits = Cache.gethitcount ();

Gets the number of hits in the in-memory cache read;
Cache cache = Manager.getcache ("Testcache");
int hits = Cache.getmemorystorehitcount ();

Gets the number of hits in the disk cache read;
Cache cache = Manager.getcache ("Testcache");
int hits = Cache.getdiskstorecount ();

Get the number of lost cache reads;
Cache cache = Manager.getcache ("Testcache");
int hits = Cache.getmisscountnotfound ();

Gets the number of lost objects that have been destroyed by the cache read;
Cache cache = Manager.getcache ("Testcache");
int hits = cache.getmisscountexpired ();

Ehcache Introduction Classic: The second ehcache.xml parameter

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.