Add Ehcache Dependency package in 1.pom.xml
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.9.1</version>
</dependency>
2. Add ehcache configuration file under Classpath ehcache.xml
<?xml version= "1.0" encoding= "UTF-8"? ><ehcache updatecheck= "false" > <diskstore path= "Java.io.tmpdir"/><!-- name : the cache name. maxelementsinmemory: Maximum number of caches. eternal: The object is permanently valid, but if set, timeout will not work. timetoidleseconds: Sets the allowable idle time (in seconds) for an object before it expires. only when eternal= False when the object is not permanently valid, an optional attribute, the default value is 0, which means that the idle time is infinite. timetoliveseconds: Sets the time (in seconds) that an object is allowed to survive before it expires. The maximum time is between the creation time and the expiration time. is only used when the Eternal=false object is not permanent, the default is 0, which means that the object survives indefinitely. overflowtodisk: When the number of objects in memory reaches Maxelementsinmemory, Ehcache writes the object to disk. &nbSP;&NBSP;DISKSPOOLBUFFERSIZEMB: This parameter sets the buffer size of the Diskstore (disk cache). The default is 30MB. Each cache should have its own buffer. maxelementsondisk: Maximum number of hard disk caches. diskpersistent: Whether to cache virtual machine Restart period Data Whether the disk store persists between restarts of the Virtual Machine. The default value is false. Diskexpirythreadintervalseconds: Disk failed thread run time interval, default is 120 seconds. memorystoreevictionpolicy: When the maxelementsinmemory limit is reached, Ehcache will clean up the memory according to the specified policy. The default policy is LRU (least recently used). You can set it to FIFO (first in, out) or LFU (less used). clearonflush: If the maximum amount of memory is cleared. --> <defaultcache maxelementsinmemory= "10000" Eternal= "false" timetoidleseconds= "60" timetoliveseconds= " " overflowtodisk= "true" maxelementsondisk= "10000000" diskpersistent= "false" diskexpirythreadintervalseconds= " " memorystoreevictionpolicy= "LRU" /> <cache name= "Qy_api_producttop" maxelementsinmemory= "10000" eternal= "false" timetoidleseconds= " timeToLiveSeconds=" 20 " overflowtodisk= "false" diskpersistent= " False " diskexpirythreadintervalseconds=" 1 "/></ehcache>
3.spring configuration file Applicationcontext.xml need to be aware of adding
red italic Partial configuration
<beans
xmlns:cache= "/http Www.springframework.org/schema/cache "
&NBSP; xsi :schemalocation = " http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd "
&NBSP; >
<!--enable the cache annotation feature, this is necessary, otherwise the annotation will not take effect, and the note must be declared in the Spring master configuration file will not take effect--
<cache:annotation-driven cache-manager= "Ehcachemanager"/>
<!--CacheManager Factory class, specifying the location of Ehcache.xml-
<bean id= "Ehcachemanagerfactory" class= "Org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >
<property name= "configlocation" value= "Classpath:ehcache.xml"/>
</bean>
<!--statement CacheManager--
<bean id= "Ehcachemanager" class= "Org.springframework.cache.ehcache.EhCacheCacheManager" >
<property name= "CacheManager" ref= "Ehcachemanagerfactory"/>
</bean>
4. Add annotation configuration at the service layer
4.1 Using Spring 3.1 Spring 3.1 and later configuration
@Cacheable
Cache a method call. The following example, the value is the return type, a Manual. The key is extracted from the ISBN argument using the ID.
@org. Springframework.cache.annotation.Cacheable (value= "Qy_api_producttop", key= "#isbn. ID") Public Manual Findmanual (ISBN ISBN, Boolean checkwarehouse)
key= "#isbn. ID" Object cache key value, need to be guaranteed unique, with Spring spel expression, value is the ID property value of the ISBN object
value= "Qy_api_producttop": refers to the cache name in ehcache.xml
also supports conditional condition, including attribute id<10, as follows:
@Cacheable (value = "Qy_api_producttop", key= "#isbn. ID", condition = "#isbn. id<10") Public Manual Findmanual (ISBN ISBN , Boolean Checkwarehouse)
@CacheEvict
Clears the cache when called. Clears all objects in the qy_api_producttop cache
@org. Springframework.cache.annotation.CacheEvict (value = "Qy_api_producttop", allentries=true) public void Loadmanuals (Long ID)
You can clear the cache for the specified object for example:
@CacheEvict (value = "Qy_api_producttop", key = "#id") public void Loadmanuals (Long ID)
4.2 Spring 2.5 to 3.1 version configuration also relies on the following jar
< Dependency >
< groupId >com.googlecode.ehcache-spring-annotations</groupId>
< Artifactid >ehcache-spring-annotations</artifactid>
< version >1.1.2</version>
< type >jar</type>
< Scope >compile</scope>
</ Dependency >
This open source, led by Eric Dalquist, predates the Spring 3.1 project. You can use it with earlier versions of Spring, or you can use it with 3.1.
@Cacheable
As with Spring 3.1 it uses an @Cacheable annotation to cache a method. In the example calls to FindMessage is stored in a cache named "Messagecache". The values are of type Message. The ID for each entry is the ID argument given.
@Cacheable (cachename = "Messagecache")
Public Message findmessage (long ID)
@TriggersRemove
And for cache invalidation, there is the @TriggersRemove annotation. In this example, Cache.removeall () are called after the method is invoked.
@TriggersRemove (cachename = "Messagescache",
when = when.after_method_invocation, RemoveAll = True)
public void AddMessage (Message message)
5. Test validation to view the printed SQL
For example, setting show_sql=true in Hibernate configuration
<prop key= "Hibernate.show_sql" >true</prop>
5.1 Access to the cache method for the first time after multiple accesses within the cache time to see if there is a SQL output, not normal for the cache
5.2 The second output time and the first time interval is greater than the time set in Ehcache.xml is greater than the cache is normal
Spring + Ehcache Annotated usage example