Ehcache is a very lightweight cache implementation, and it supports the cluster since 1.2 and is the default cache provider for hibernate. EhCache is a pure Java in-process caching framework, which is fast and capable, and is the default Cacheprovider in Hibernate.
Ehcache's distributed cache is a traditional rmi,1.5 version of the jgroups,1.6 version of JMS. Distributed cache mainly solves the problem of synchronizing data between different servers in the cluster environment.
Using spring's AOP for consolidation, you can flexibly cache the returned result objects of a method.
The Cachingfilter feature can cache the contents of an HTTP response.
1. Main Features
1) fast.
2) simple.
3) Multiple cache policies
4) cache data has two levels: memory and disk, so there is no need to worry about capacity issues
5) cache data is written to disk during virtual machine restart
6) distributed caching can be done via RMI, pluggable APIs, etc.
7) Listening interface with cache and cache manager
8) Support multiple cache manager instances, and more than one instance of the cache area
9) provides cache implementation of Hibernate
10) etc.
2 . When using Ehcache, you need to add the following configuration to the spring configuration:
<?XML version= "1.0" encoding= "UTF-8"?>
<!--1) cache namespace;--><BeansXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://www.springframework.org/schema/beans"Xmlns:cache= "Http://www.springframework.org/schema/cache"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/ Spring-cache.xsd ">
<!--2) enable the cache annotation feature, otherwise it cannot be cached by annotations;-->
<cache-manager= "CacheManager"/>
<!--3) The following configuration uses a spring-provided ehcachecachemanager to generate a spring CacheManager that receives a ehcache CacheManager because it is actually used to cache the number of It is still the ehcache. The CacheManager of Ehcache is generated through the Ehcachemanagerfactorybean provided by spring and can be generated by specifying the location of the Ehcache configuration file to generate a Ehcache CA chemanager. If not specified, the Ehcache.xml file under the Classpath root path is taken according to the default rule of Ehcache, and if the file does not exist, the ehcache-failsafe.xm L file in the corresponding jar package is obtained as the configuration file. -
<BeanID= "Ehcachemanagerfactory"class= "Org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> < Propertyname= "Configlocation"value= "Classpath:ehcache.xml"/> </Bean> <BeanID= "CacheManager"class= "Org.springframework.cache.ehcache.EhCacheCacheManager"> < Propertyname= "CacheManager"ref= "Ehcachemanagerfactory"/> <!--Indicates whether the transaction is wrapped, and if true, if the transaction is rolled back, the cache is also rolled back, by default false - < Propertyname= "Transactionaware"value= "true"/> </Bean></Beans>
The above configuration uses a spring-provided ehcachecachemanager to generate a spring CacheManager that receives a ehcache CacheManager, Because it's ehcache to actually cache the data. The CacheManager of Ehcache is generated through the Ehcachemanagerfactorybean provided by spring, which can generate a Ehcache CacheManager by specifying the location of the Ehcache configuration file. If not specified, the ehcache.xml file under the Classpath root path will be taken according to the default rule of Ehcache, and if the file does not exist, get ehcache in the corresponding jar package Ehcache-failsafe.xml file as a configuration file. More about Ehcache here is not much to say, it does not belong to the content of this article, to learn more about Ehcache can refer to my previous release of the Ehcache series of articles, can also refer to official documents.
3, ehcache.xml configuration format Description:
Diskstore:
Specifies a file directory where the data is written to the file directory when Ehcache writes the data to the hard disk;
The values are:
user.home– User Home directory
user.dir– User's current working directory
java.io.tmpdir– Default temporary file path
Defaultcache:
Set the default data expiration policy for the cache
Cache:
Set the specific data expiration policy for the named cache;
the properties contained in the Cache are mainly:
Maxelementsinmemory: Maximum number of elements that can be cached;
Eternal:True indicates that the object never expires, the Timetoidleseconds and Timetoliveseconds properties are ignored, and the default is False
Overflowtodisk:trueindicates that when the cached data reaches maxelementsinmemory , the overflow object is cached to disk; false , it will be retired according to the expiration policy;
Timetoidleseconds: The maximum time the object is idle, in seconds, at which time the object expires;
Timetoliveseconds: The maximum time, in seconds, that a set object is allowed to exist in the cache;
Diskpersistent: no persistence on disk. Indicates whether the Data is valid after restarting the JVM.
<?XML version= "1.0" encoding= "UTF-8"?><Ehcache> <DiskstorePath= "Java.io.tmpdir"/> <Defaultcachemaxelementsinmemory= "10000"Eternal= "false"Overflowtodisk= "false"Timetoidleseconds= "1800"Timetoliveseconds= "1800"diskpersistent= "false"/> <Cachename= "Bdrules"maxelementsinmemory= "$"Eternal= "false"Overflowtodisk= "false"Timetoidleseconds= "$"Timetoliveseconds= "$"Memorystoreevictionpolicy= "LFU" /> <Cachename= "Bdrulescount"maxelementsinmemory= "$"Eternal= "false"Overflowtodisk= "false"Timetoidleseconds= "$"Timetoliveseconds= "$"Memorystoreevictionpolicy= "LFU" /> <Cachename= "Bdmrules"maxelementsinmemory= " the"Eternal= "false"Overflowtodisk= "false"Timetoidleseconds= "$"Timetoliveseconds= "$"Memorystoreevictionpolicy= "LFU" /> </Ehcache>
4, note introduction:
1) cacheable: Add cache, if already cached, do not execute method, return cached result directly
Value: Cache name, not empty, consistent with the name of the cache declared in Ehcache.xml
Key: Cache key, default is empty, use method parameter type and parameter value as key, support Spel
Condition: A trigger condition that caches the return value of a method when the condition is met, defaults to null, means that all are added to the cache, and supports Spel
@Cacheable (value = "Bdmrules", key = "#p0") salecommissionrulebdmentity Querybdmrulebymonth (@Param ("TimeInterval") String timeinterval, @Param ("department") string department);
2) Cacheevict: Execute method, and clear cache
Value: Cache location name, cannot be empty
Key: Cache key, default is null, ibid.
Condition: Trigger condition, the cache is cleared only if conditions are met, default is empty, support spel
Allentries:true indicates that all caches in value are cleared, false by default
3) Cacheput: To ensure that the method is executed and the results are cached
Parameters with cacheable
ehcache--annotation-based cache usage