Introduction: This project builds on spring4.x, using ehcache3.5.2 and Jcache (jsr107 specification) I, dependency In addition to Ehcache and CACHE-API, note the reference Spring-context-support
<dependency> <groupid>org.springframework</ Groupid>
<artifactId>spring-context-support</artifactId> <version>4.3.16.RELEASE</version> </ Dependency> &Nbsp;<dependency> <groupId>org.ehcache</groupId> &
Nbsp;<artifactid>ehcache</artifactid> <version>3.5.2</version>
</dependency> <dependency> <groupId>javax.cache</groupId> <artifactId>cache-api</artifactId> <version> 1.0.0</version> </dependency>
Second, the configuration
1. Ehcache Configuration
<?xml version= "1.0" encoding= "UTF-8"?> <ehcache:config xmlns:ehcache= "Http://www.ehcache.org/v3" XMLNS:JC Ache= "http://www.ehcache.org/v3/jsr107" > <ehcache:service> <jcache:defaults> <jcache:cache Name= "Default" template= "Mydefaulttemplate"/> </jcache:defaults> </ehcache:service> <EHCACHE:CA Che alias= "Allcameracache" > <ehcache:key-type copier= "Org.ehcache.impl.copy.SerializingCopier" > java.lang.string</ehcache:key-type> <ehcache:value-type copier= "Org.ehcache.impl.copy.SerializingCopier ">java.lang.String</ehcache:value-type> <ehcache:expiry> <ehcache:tti unit=" Minutes ">20< /ehcache:tti><!--Data Expiration time 20 minutes-</ehcache:expiry> <ehcache:heap unit= "Entries" >200</EHCAC he:heap><!--cache up to 200 objects-</ehcache:cache> <!--using templates, you can override the properties of the template--<ehcache:cache alias= "CA Meracache "uses-template=" Mydefaulttemplate "> <ehcache:key-type>java.lang.Object</ehcache:key-type> <ehcache:value-type> java.lang.object</ehcache:value-type> <ehcache:expiry> <ehcache:tti unit= "Minutes" >30</ ehcache:tti><!--Data Expiration time 30 minutes, overriding template default Properties-</ehcache:expiry> <ehcache:heap unit= "Entries" >500&L t;/ehcache:heap><!--cache up to 500 objects-</ehcache:cache> <!--default Template--<ehcache:cache-templat E name= "Mydefaulttemplate" > <ehcache:expiry> <ehcache:none/><!--Cache never expires-</ehcach E:expiry> </ehcache:cache-template> </ehcache:config>
2. Spring Configuration
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:
Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:cache= "Http://www.springframework.org/schema/cache" xsi:schemalocation= "http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/ Context Http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/ Cache http://www.springframework.org/schema/cache/spring-cache.xsd "> <!--eguid blog address: http://bog.eguid.cc-- > <cache:annotation-driven cache-manager= "CacheManager"/><!--scan the cache annotations, if one is already available--and <context: Component-scan base-package= "Cc.eguid.cache"/><!--Scan path-<!--jcache Cache--<bean id= " Jcachemanager "class=" Org.springframework.cache.jcache.JCacheManagerFactoryBean "> &nbSp; <property name= "Cachemanageruri" value= "Classpath:config/ehcache.xml"/> <!--changed to the path of the configuration file-- </bean> <bean id= "CacheManager" class= "Org.springframework.cache.jcache.JCacheCacheManager" > <prope
Rty name= "CacheManager" ref= "Jcachemanager"/> </bean> </beans>
third, make the cache effective 1. Use the annotation method
@Cacheable (value= "Cameracache", key= "#userid")
public string Getcameralist (string Userid,integer otherparam) {
...
}
Spring-cache's annotations are relatively simple and will not be mentioned again.