Ehcache Introduction
Ehcache is a pure Java in-process caching framework.
With the Ehcache caching framework, you can store data in the cache first, with two levels of cache data: memory and disk, so there is no need to worry about capacity issues. The cached data is written to disk during the virtual machine restart. The caching framework is used primarily faster than traditional databases.
Ehcache can work well when the amount of data is not very large, such as small crawlers with data volumes at level 100,000, which can be used to judge duplicate URLs.
Import dependency
To import dependencies from the maven remote repository , it is recommended to choose a more stable 2. X version
<Dependencies> <Dependency> <groupId>Net.sf.ehcache</groupId> <Artifactid>Ehcache</Artifactid> <version>2.10.3</version> </Dependency></Dependencies>
Configuration file
You first need to create a configuration file under the Src/main/resources directory Ehcache.xml
Ehcach Common Configuration Items
properties of the cache element:
Name : Cache names
maxelementsinmemory: Maximum number of cached objects in memory
maxelementsondisk: The maximum number of cache objects on the hard disk, if 0 means infinity
Eternal: True indicates that the object never expires, the Timetoidleseconds and Timetoliveseconds properties are ignored, and the default is False
Overflowtodisk: True means that when the number of objects in the memory cache reaches the maxelementsinmemory limit, the overflow object is written to the hard disk cache. Note: If the cached object is to be written to the hard disk, the object must implement the Serializable interface.
diskspoolbuffersizemb: Disk buffer size, default is 30MB. Each cache should have its own cache.
diskpersistent: Whether to cache virtual machine Restart period Data
diskexpirythreadintervalseconds: Disk failed thread run time interval, default is 120 seconds
timetoidleseconds: Sets the maximum time, in seconds, that an object is allowed to be idle. When an object has been idle for longer than the Timetoidleseconds property value since the last time it was accessed, the object expires and Ehcache will empty it from the cache. This property is valid only if the Eternal property is false. If the property value is 0, the object can be idle indefinitely
timetoliveseconds: The maximum amount of time, in seconds, that a set object is allowed to exist in the cache. When an object has been stored in the cache for more than the Timetoliveseconds property value, the object expires and Ehcache clears it from the cache. This property is valid only if the Eternal property is false. If the property value is 0, the object can exist indefinitely in the cache. Timetoliveseconds must be greater than timetoidleseconds attribute to make sense
memorystoreevictionpolicy: When the maxelementsinmemory limit is reached, Ehcache will clean up the memory according to the specified policy. The optional policies are LRU (least recently used, default policy), FIFO (first in, out), LFU (minimum number of accesses).
Configure Ehcache.xml
<?XML version= "1.0" encoding= "UTF-8"?> <Ehcache> <!--disk Storage: Transfer objects temporarily unused in the cache to the hard disk, similar to the virtual memory of the Windows system path: Specifies the path of the object to be stored on the hard disk - <DiskstorePath= "C:\ehcache" /> <!--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: Represents whether an object never expires Overflowtodisk: When the number of element in memory reaches Maxelementsinmemory, Ehcache will write the element to disk - <Defaultcachemaxelementsinmemory= "+"Eternal= "true"Overflowtodisk= "true"/> <!--Maxelementsinmemory set to 1,overflowtodisk set to true, as long as there is a cache element, it is stored directly to the hard disk eternal set to True, representing the object is permanently valid Maxel Ementsondisk set to 0 indicates the maximum number of cache objects on the hard disk infinite diskpersistent set to TRUE indicates cache virtual machine Restart period Data - <Cachename= "a"maxelementsinmemory= "1"Eternal= "true"Overflowtodisk= "true"Maxelementsondisk= "0"diskpersistent= "true"/> </Ehcache>
Instance Code
PackageCN.CSLG;ImportNet.sf.ehcache.Cache;ImportNet.sf.ehcache.CacheManager;Importnet.sf.ehcache.Element; Public classEhcachetest { Public Static voidMain (string[] args) {//to create a cache manager from a ehcache.xml configuration fileCacheManager manager=cachemanager.create ("./src/main/resources/ehcache.xml"); Cache C=manager.getcache ("a");//gets the specified cacheElement e=NewElement ("name", "Zhangsan"); C.put (e); //add an element to the cacheElement E2=c.get ("name");//get cache elements based on keySYSTEM.OUT.PRINTLN (E2);//Auto ToStringSystem.out.println (E2.getobjectvalue ());//Print Object ValuesC.flush ();//Refresh CacheManager.shutdown ();//Close the cache manager }}
Ehcache Caching Framework