Ehcache (04)--Set the size of the cache

Source: Internet
Author: User

Set the size of the cache

Directory

1 CacheManager level

2 Cache level

3 Size measurement

4 Sample Configuration Size

The cache size limit can be set on the CacheManager, or on individual caches. We can set the cache to use the size of the memory, you can also set the cache to use the size of the disk, but the use of heap memory is required to set the size, the other can be set to not set, the default is unrestricted. when setting the cache size, we can set the cache to use the maximum number of bytes of a certain memory, or you can set the maximum number of storage elements on a certain memory.

1 CacheManager level

The CacheManager level has three properties that can be used to limit the size of three memory cache information, which is controlled by the number of bytes, maxbyteslocalheap,maxbyteslocaloffheap , and Maxbyteslocaldisk. The size of the CacheManager level limit is shared by all caches in it.

maxbyteslocalheap is used to limit the maximum number of bytes of heap memory that the cache can use, in units of K, M, or g, not case sensitive. The default is 0, which means no limit. But when we do not specify a CacheManager level of maxbyteslocalheap, we must specify MAXBYTESLOCALHEAP or Maxentrieslocalheap on each cache.

maxbyteslocaloffheap is the maximum number of bytes of non-heap memory that can be used to limit the cache, and its units can be k, m, or G. The default is 0, which means no limit. But when we specify maxbyteslocaloffheap at the CacheManager level, we implicitly make all cache levels overflowtooffheap true. Using this property at the cache level only causes the current cache's overflowtooffheap to be true. If Overflowtooffheap is not required at this point, then we need to display the specified overflowtooffheap at the cache level as false. Only Enterprise editions of Ehcache can use non-heap memory to store cache information.

Maxbyteslocaldisk is used to limit the maximum number of bytes of disk that the cache can use, in units of K, M, or G. The default is 0, which means no limit. Local disks can only be used in a standalone environment and are not available in a distributed environment. In addition, this setting only applies when the information in the cache is temporarily written to disk, and is not applicable for persisting cache information to disk. The same is true for Maxbyteslocaldisk at the cache level. When we specify maxbyteslocaldisk at the CacheManager level, the overflowtodisk of all caches is implicitly specified as true, The cache level only causes the current cache's Overflowtodisk to be true.

The following is an example of specifying a cache size limit at the CacheManager level, in which we specify that the maximum heap memory that the CacheManager can use is 500M, the maximum non-heap memory is 2G, and the maximum number of bytes using the local disk is 50G.

<xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"   xsi: noNamespaceSchemaLocation= "http://ehcache.org/ehcache.xsd"   maxbyteslocalheap= " 500M "  maxbyteslocaloffheap=" 2G "  maxbyteslocaldisk=" 50G "  ></ehcache>

2 Cache level

The limit size parameters that can be specified on CacheManager are available at the cache level.

When we specify a certain type of limit size at the cache level, the cache will no longer share that restriction within CacheManager. If we limit the maximum heap memory usage at the CacheManager level maxbyteslocalheap to 1G, it means that all caches within CacheManager will share this 1G heap of memory, If this time we specify that the maximum number of cache1 in CacheManager is maxbyteslocalheap to 200M, it means that the maximum heap memory usage of CACHE1 is only 200M of its own setting. The other caches can only share the remaining 824M.

       Unlike CacheManager, we are in cache You can also use on CacheManager. For example, our CacheManager has the following configuration, we have specified that its maximum heap memory is 500M, then we define a cache at this time, when we specify its maxbyteslocalheap we can specify its value as a percentage, example 50%, indicates that our cache uses up to 50% of the CacheManager level of maxbyteslocalheap, which is the 250M . And if we were to specify maxbyteslocaldisk when we tried to use percentages on the cache, we would have an error, because we didn't specify maxbyteslocaldisk at the CacheManager level, Ehcache will not know what percentage you are in the end.

<xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"   xsi: noNamespaceSchemaLocation= "http://ehcache.org/ehcache.xsd"   maxbyteslocalheap= " 500M "></ehcache>

In addition, at the cache level we can also use two properties to limit the maximum number of cached elements in heap memory or on disk, both Maxentrieslocalheap and Maxentrieslocaldisk. For non-heap memory offheap, the maximum number of elements cannot be specified.

maxentrieslocalheap is used to limit the maximum number of elements that the current cache can hold on heap memory. Ehcache stipulates that maxbyteslocalheap or MAXENTRIESLOCALHEAP must be specified on each cache if MAXBYTESLOCALHEAP is not specified on CacheManager But Maxentrieslocalheap and Maxbyteslocalheap cannot appear at the same time. That means we can't specify both Maxbyteslocalheap and Maxentrieslocalheap on a cache, Of course we cannot specify maxentrieslocalheap on the cache and specify Maxbyteslocalheap on CacheManager. However, specifying MAXBYTESLOCALHEAP on both the CacheManager and the cache is allowed.

Maxentrieslocaldisk is used to limit the maximum number of elements that can be saved on a disk. (For the time being, do you think this kind of storage limit should correspond to persistence ?) ).

3 Size measurement

The elements placed in the cache will be sized using net.sf.ehcache.pool.sizeof.SizeOf, which is only for elements that exist in the heap memory , because non-heap memory and disk are The bytes that are stored directly can be computed directly. the size of the element includes the key and value of the element, as well as other property information, and the information is recursively computed when the size is calculated, that is, if value is associated with another object B, the size of B is also counted. If we do not want to calculate the size of something inside an element, we can use the net.sf.ehcache.pool.sizeof.annotations.IgnoreSizeOf annotation to mark the size of the content being ignored. ignoresizeof does not consider inheritance, that is, if you label ignoresizeof on Type A, you will only ignore the size of a, not the size of the subclass B of a, and if you need to ignore the size of B, You need to also use ignoresizeof for labeling on Class B.

Ignoresizeof can be tagged in three places.

(1) on a property : Indicates that the corresponding property of an object is ignored. This means that we will ignore the role property of the user object.

     Public class User {                @IgnoreSizeOf         private  role role;              }  

(2) on class : Indicates that the type is ignored. The following indicates that we will ignore the user type.

    @IgnoreSizeOf      publicclass  User {                private String name;              }  

(3) on the package : Marking on a package is marked on the Package-info.java of the package, indicating that all of the following types of the package are ignored, such as when we want to ignore all content under the COM.XXX.XXX package, you can use ignore on the package in the Package-info.java file that corresponds to the packet. sizeof annotations are annotated.

    @net. sf.ehcache.pool.sizeof.annotations.IgnoreSizeOf      package com.xxx.xxx;  

In addition, we can specify which properties, classes, and packages need to be ignored when calculating the size of the elements in the cache by fully qualified names, then write the information in a property file, and then point the System property net.sf.ehcache.sizeof.filter to the file. System properties can be specified by Java–dnet.sf.ehcache.sizeof.filter.

As mentioned above, our sizeof is a recursive measure of size, that is, it calculates the references that the object holds, references, and so on. For this, we can specify that sizeof is a strategy for sizing, sizeofpolicy. Sizeofpolicy have CacheManager level and cache level, respectively, corresponding to Ehcache child element and cache child element or Defaultcache child element. When both the cache level and the CacheManager level are specified with Sizeofpolicy, the cache level configuration overrides the CacheManager level configuration. The Sizeofpolicy element has two properties, MaxDepth and Maxdepthexceededbehavior.

L MaxDepth represents the maximum depth of the link.

L Maxdepthexceededbehavior indicates that when the depth of the visit exceeds the specified maxdepth behavior, there are two types of continue and abort. Continue will give a warning, then continue to calculate the size, which is the default, abort will give a warning, then abort this calculation, and mark the use of memory that is not tracked.

4 Sample Configuration Size

(1) The size limit is specified at the CacheManager level only, and is not specified at the cache level, in which case cache1, cache2, cache3 divide the size limit of the CacheManager level. That is, each has a cachemanager level of heap, offheap, and disk one-third capacity.

<EhcacheXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:nonamespaceschemalocation= "Http://ehcache.org/ehcache.xsd"Maxbyteslocalheap= "500M"Maxbyteslocaloffheap= "5G"Maxbyteslocaldisk= "50G">     <Cachename= "Cache1"/>   <Cachename= "Cache2"/>   <Cachename= "Cache3"/> </Ehcache>

(2) The CacheManager level specifies the size limit, and some caches also specify a size limit. As below, we have specified Cache1 's maxbyteslocalheap to 200M, this time cache1 will be able to use the heap memory to 200M, and Cache2 and Cache3 will split the CacheManager level to remove the heap memory that Cache1 owns, which is 150M. The CacheManager level of offheap and disk capacity will be divided by Cache1, Cache2 and Cache3, each one-third.

<EhcacheXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:nonamespaceschemalocation= "Http://ehcache.org/ehcache.xsd"Maxbyteslocalheap= "500M"Maxbyteslocaloffheap= "5G"Maxbyteslocaldisk= "50G">     <Cachename= "Cache1"Maxbyteslocalheap= "200M"/>   <Cachename= "Cache2"/>   <Cachename= "Cache3"/> </Ehcache>

(3) CacheManager does not specify a size limit, the cache level specifies the size limit. This time will be used each, that is, each cache can only use its own specified size.

<EhcacheXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:nonamespaceschemalocation= "Http://ehcache.org/ehcache.xsd">     <Cachename= "Cache1"Maxbyteslocalheap= "200M"/>   <Cachename= "Cache2"Maxbyteslocalheap= "300M"/>   <Cachename= "Cache3"Maxbyteslocalheap= "200M"/> </Ehcache>

(4) When we specify a size limit at the CacheManager level, we can specify the corresponding size limit that can be used at the cache level by a percentage. In the example below, our CacheManager has 500M of heap memory, 5G of offheap memory, and 50G of disk capacity. Cache1 has the heap memory of CacheManager 20%, which is 100M, the remaining 400M Cache2 and Cache3 are equally divided, that is 200m;cache2 has CacheManager 20% of offheap memory, that is, 1G, The remaining 4G will be divided by Cache1 and Cache3, that is, each 2g;cache3 has CacheManager 20% disk capacity, that is 10G, the remaining 40G will be divided by Cache1 and Cache2, that is, 20G. So eventually Cache1 will have heap 100M, offheap 2G, disk 20g;cache2 will have heap 200M, offheap 1G, disk 20g;cache3 will have the heap 200M, offheap 2G, disk 10G.

<EhcacheXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:nonamespaceschemalocation= "Http://ehcache.org/ehcache.xsd"Maxbyteslocalheap= "500M"Maxbyteslocaloffheap= "5G"Maxbyteslocaldisk= "50G">     <Cachename= "Cache1"Maxbyteslocalheap= "20%"/>   <Cachename= "Cache2"Maxbyteslocaloffheap= "20%"/>   <Cachename= "Cache3"Maxbyteslocaldisk= "20%"/> </Ehcache>

5) when we were not in CacheManager the heap memory, Cache1, Cache2 memory, so it will be 200M Heap memory. and Cache1 memory; Cache2 also specifies that 20G can be used only Cachemanager memory.

<EhcacheXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:nonamespaceschemalocation= "Http://ehcache.org/ehcache.xsd"Maxbyteslocalheap= "600M">     <Cachename= "Cache1"Maxbyteslocaloffheap= "2G"/>   <Cachename= "Cache2"Maxbyteslocaldisk= "20G"/>   <Cachename= "Cache3"/> </Ehcache>

In short, when the cache itself sets a certain capacity limit, the capacity limit of the corresponding type will use its own restrictions, such as a cache itself set Maxbyteslocalheap to 100M, then it can use the largest amount of heap memory is 100M. When the cache itself does not specify a certain type of capacity limit, if it is specified at the CacheManager level, Then the cache will be split equally with other caches that do not specify the type's capacity limit CacheManager level the capacity of this type is assigned the remaining capacity after the cache partition of that type's capacity limit, otherwise it will not be available for that type of capacity. If Cachea does not specify a maxbyteslocaldisk limit, and the CacheManager level specifies a maxbyteslocaldisk limit of 10G, If there is another cacheb within the CacheManager that specifies a maxbyteslocaldisk limit of 5G, then Cachea will be able to use a disk capacity of 5G, If there are other N caches at this time and do not specify the Maxbyteslocaldisk limit, then they will be together with Cachea partition CacheManager disk capacity 10G to remove the Cacheb disk capacity 5G after the remaining 5G, that is, each 5/(N +1) G. If the CacheManager level also does not specify Maxbyteslocaldisk, Then Cachea and other caches that do not specify Maxbyteslocaldisk will not be able to store cache information using disk (assuming that the caches also do not specify Maxentrieslocaldisk). If you do not understand this piece, you can refer to the example in section four, "Sample configuration size."

(This article is written based on Ehcache2.8.1)

Ehcache (04)--Set the size of the cache

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.