Cache series: Spring-cache simple three-step fast application Ehcache3.x-jcache cache (spring4.x)

Source: Internet
Author: User
Tags xmlns

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> &LT;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" &GT;200&LT;/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" &GT;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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.