The cache configuration is flexible and there are several official cache configurations available. You can pass in different parameters by declaring the configuration, configuring it in XML, configuring it in a program, or invoking a constructor method.
You can split the cache configuration from the code, or you can use runtime configuration, so-called run-time configuration is nothing more than configuring it in code. The following are the benefits of run-time configuration:
· Configure all caches in the same place, which makes it easy to manage the memory and disk consumption of the cache. You can change the cache configuration when publishing.
The Ehcache.xml configuration file will be elaborated in detail in this article. An existing ehcache.xml can be copied at the time of configuration.
Code<Ehcache> <DiskstorePath= "Java.io.tmpdir"/> <Defaultcachemaxelementsinmemory= "10000"Eternal= "false"Timetoidleseconds= "+"Timetoliveseconds= "+"Overflowtodisk= "true"Maxelementsondisk= "10000000"diskpersistent= "false"Diskexpirythreadintervalseconds= "+"Memorystoreevictionpolicy= "LRU" /> </Ehcache>
Ehcache.xml and other configuration files
In previous versions of Ehcache-1.6, only ASCII-encoded ehcache.xml profiles were supported. In later versions of Ehcach-1.6, the UTF8 encoded ehcache.xml configuration file is supported. Because of backwards compatibility, all the ASCII-encoded profiles are completely unnecessary to convert to UTF8.
A cachemanager must have an XML configuration. Because of a disk path or listening port, errors occur when multiple CacheManager use the same configuration file.
· CacheManager Configuration
dmulticastgroupport=4446, so you can configure the listening port.
· Diskstore Configuration
If you are using the Diskstore (disk cache), you must configure the Diskstore configuration item. If not configured, Ehcache will use Java.io.tmpdir.
The "path" property of Diskstroe is used to configure the physical path used by the disk cache, and the file suffix used by the Ehcache disk cache is. Data and. Index.
<path= "Java.io.tmpdir"/>
· Cachemanagereventlistener Configuration
We can instantiate a cachemanagerpeerprovider through cachemanagereventlistenerfactory, and when we added and removed caches from CacheManager, Will notify the Cachemanagerpeerprovider, so that we can easily cachemanager in the cache to do some statistics.
The event listener class names registered to CacheManager are: adding a cache and removing a cache
<classproperties= ""/>
· Cachemanagerpeerprovider Configuration
CacheManager configuration Cachemanagerpeerproviderfactory Create Cachemanagerpeerprovider in the cluster. The specific examples are as follows:
< Cachemanagerpeerproviderfactoryclass = "Net.sf.ehcache.distribution." Rmicachemanagerpeerproviderfactory " properties=" peerdiscovery=manual, rmiurls=//server1:40000/ samplecache1|//server2:40000/samplecache1| Server1:40000/samplecache2|//server2:40000/samplecache2 " propertyseparator=", "/ >
· Cachemanagerpeerlistener Configuration
The Cachemanagerpeerlistener configuration is used to listen for the distribution of cached messages in the cluster.
< cachemanagerpeerlistenerfactory class = "Net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" Properties= "hostname=fully_qualified_hostname_or_ip, port=40001, sockettimeoutmillis =120000 " propertyseparator=", "/>
· Cache configuration
· Name:cache's unique identity · Maxelementsinmemory: The maximum number of cached objects in memory. Maxelementsondisk: The maximum number of cache objects on disk, or 0 for infinity. Eternal:element is permanently valid, one but set, timeout will not work. Overflowtodisk: Configuring this property, Ehcache will write element to disk when the number of element in memory reaches Maxelementsinmemory. Timetoidleseconds: Sets the allowable idle time before the element expires. The optional attribute is used only when element is not permanently valid, and the default value is 0, which means that the idle time is infinite. Timetoliveseconds: Sets the element to allow survival time before failure. The maximum time is between the creation time and the expiration time. Used only when element is not permanently valid, the default is 0, which means that element has an infinite survival time. Diskpersistent: Whether to cache the virtual machine restart period data. (This virtual machine refers to what the virtual machine has not seen what is, there is a tall man also want to be able to pointing twos). Diskexpirythreadintervalseconds: Disk failed thread run time interval, default is 120 seconds. DISKSPOOLBUFFERSIZEMB: This parameter sets the buffer size of the Diskstore (disk cache). The default is 30MB. Each cache should have its own buffer.
· Cache Exception Handling Configuration
<class= "Com.example.ExampleExceptionHandlerFactory" Properties = "Loglevel=fine" />
Summarize
This is only a detailed description of the configuration of the general-purpose cache, and the RMI cache and the cluster cache can be referenced here.
Here are a few examples of configurations:
· Ehcache Default Cache configuration
SAMPLECACHE1 Configuration
Simple configuration, there is this configuration in the Ehcache.xml file, it is best to remove it before using Ehcache, and configure it yourself.
The cache name SampleCache1, which caches up to 10,000 element in memory, where the element expires after 5 minutes of inactivity or 10 minutes of survival.
At more than 10000element, the element is output to disk and the output path is java.io.tmpdir.
· SampleCache2 Configuration
The cache, named SampleCache2, can cache up to 1000 element in memory and beyond 1000 cannot be output to disk. The cache is permanently valid.
<name= "SampleCache2" maxelementsinmemory= "$" Eternal = "true" overflowtodisk= "false" memorystoreevictionpolicy= "FIFO" />
· SAMPLECACHE3 Configuration
The cache name is SampleCache3. Can be cached to disk. The disk cache caches data for the virtual machine restart period. The disk cache failed thread runs at 10 minute intervals.
· SAMPLEDISTRIBUTEDCACHE1 Configuration
· SampleDistributedCache2 Configuration
· SAMPLEDISTRIBUTEDCACHE3 Configuration
<Cachename= "SampleDistributedCache2"maxelementsinmemory= "Ten"Eternal= "false"Timetoidleseconds= "+"Timetoliveseconds= "+"Overflowtodisk= "false"> <cacheeventlistenerfactoryclass= "Net.sf.ehcache.distribution.RMICacheReplicatorFactory"Properties= "Replicateasynchronously=false, Replicateputs=false, Replicateupdates=true, REPLICATEUPDATESV Iacopy=true, Replicateremovals=false "/> </Cache>
Code<!--Sample Distributed cache named SampleDistributedCache3. This cache replicates using defaults except, the asynchronous replication interval is set to 200ms. - <Cachename= "SampleDistributedCache3"maxelementsinmemory= "Ten"Eternal= "false"Timetoidleseconds= "+"Timetoliveseconds= "+"Overflowtodisk= "false"> <cacheeventlistenerfactoryclass= "Net.sf.ehcache.distribution.RMICacheReplicatorFactory"Properties= "asynchronousreplicationintervalmillis=200"/> </Cache>
<Cachename= "SampleCache3"maxelementsinmemory= "$"Eternal= "false"Overflowtodisk= "true"Timetoidleseconds= "+"Timetoliveseconds= "All"diskpersistent= "true"Diskexpirythreadintervalseconds= "1"Memorystoreevictionpolicy= "LFU" />
the cache name is sampleDistributedCache1. <Cachename= "SampleDistributedCache1"maxelementsinmemory= "Ten"Eternal= "false"Timetoidleseconds= "+"Timetoliveseconds= "+"Overflowtodisk= "false"> <cacheeventlistenerfactoryclass= "Net.sf.ehcache.distribution.RMICacheReplicatorFactory"/> <bootstrapcacheloaderfactoryclass= "Net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/> </Cache>
<Defaultcachemaxelementsinmemory= "10000"Eternal= "false"Timetoidleseconds= "+"Timetoliveseconds= "+"Overflowtodisk= "true"Diskspoolbuffersizemb= "+"Maxelementsondisk= "10000000"diskpersistent= "false"Diskexpirythreadintervalseconds= "+"Memorystoreevictionpolicy= "LRU" />
<Cachename= "SampleCache1"maxelementsinmemory= "10000"Maxelementsondisk= "+"Eternal= "false"Overflowtodisk= "true"Diskspoolbuffersizemb= " the"Timetoidleseconds= "+"Timetoliveseconds= "All"Memorystoreevictionpolicy= "LFU" />
Ehcache Cache Configuration