Ehcache is an open source code based on a standard high-speed cache system.
There are many usage configurations for ehcache on the Internet, but they are generally based on configuration files. But in actual application. We may need to dynamically manage the cache, so simply configuring the file is not enough.
Therefore, we need to configure the cache in the encoding format.
In fact, ehcache supports hard-coding to create configurations (the configuration file is only a form and eventually needs to be parsed into a Java class model ).
Here we can compare the differences between the two ehcache cache creation methods.
The first method is to create a configuration using Java code without using the configuration file.
Configuration configuration = new configuration ()//. diskstore (New diskstoreconfiguration (). PATH ("Java. io. tmpdir ") // temporary file directory // specify the list of hosts that provide synchronization in the network group other than itself, and separate different hosts with" |. cachemanagerpeerproviderfactory (New factoryconfiguration <?> ()//. Classname (rmicachemanagerpeerproviderfactory. class. getname ())//. properties ("peerdiscovery = manual, rmiurls = // localhost: 40004/metacache | // localhost: 40005/metacache") // configure the listener for the host. cachemanagerpeerlistenerfactory (New factoryconfiguration <?> ()//. Classname (rmicachemanagerpeerlistenerfactory. class. getname ())//. properties ("Port = 40004, sockettimeoutmillis = 2000 ")//)//. cache (New cacheconfiguration ("metacache", 10000) // The cache name (which must be unique). The maximum number of elements that can be stored in maxelements memory. memorystoreevictionpolicy (memorystoreevictionpolicy. LFU) // Cleaning Mechanism: LRU uses at least FIFO first-in-first-out LFU recently. timetoidleseconds (1000) // maximum idle time of the element. timetoliveseconds (2000) // maximum survival time of the element. eternal (false) // whether the element is permanently cached. Diskexpirythreadintervalseconds (120) // cache cleaning time (120 s by default) // localtempswap when the cache capacity reaches the upper limit, the cache object (including heap and non-heap) switch to disk // none when the cache capacity reaches the upper limit, the cache object (including heap and non-heap) switch to the disk // distributed is executed in the persistent mode configured by the _ terracotta tag. This option is unavailable for non-distributed deployment. persistence (New persistenceconfiguration (). strategy (persistenceconfiguration. strategy. none )). maxentrieslocaldisk (0) // The maximum number of cached objects on the disk. 0 indicates infinity ). cacheeventlistenerfactory (New cacheconfiguration. cacheeventlistenerfactoryconfiguration (). classname (rmicachereplicatorfactory. class. getname () //); cachemanager manager = cachemanager. create (configuration); cache = manager. getcache ("metacache"); // get the cache
(The configuration is written together with the concatenation, and can be written separately in actual applications)
Second, use the configuration file. (There are a lot of online materials in this method. The following examples are mainly compared with the above examples)
Ehache. xml
<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true"> <diskStore path="java.io.tmpdir" /> <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=manual,rmiUrls=//localhost:40004/metaCache|//localhost:40005/metaCache" /> <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" properties="port=40004,socketTimeoutMillis=2000" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> </defaultCache> <cache name="metaCache" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="2000" timeToLiveSeconds="1000" overflowToDisk="false"> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" /> </cache></ehcache>
Use cache in Java
URL url = getclass (). getresource ("ehache. XML "); cachemanager manager = new cachemanager (URL); cache = manager. getcache ("metacache"); // get the cache