Spring boot Spring Cache ehcache3.x Integration

Source: Internet
Author: User

http://blog.csdn.net/qq18998401056/article/details/53467671

**************************************************************************

In spring boot, the appropriate cache manager (CacheManager) is configured with @enablecaching annotation automation, and Spring boot detects the cache provider according to the following sequence:
Generic
Jcache (JSR-107)
EhCache 2.x
Hazelcast
Infinispan
Redis
Guava
Simple
In addition to sequential detection, we can also force the designation by configuring the property Spring.cache.type. The default is simple type.
Because ehcache3.x implements the standard interface for jsr-107, this article uses Jcache as a way to integrate ehcache3.x.
The following dependencies are introduced:

<Dependency>          <groupId>Org.ehcache</groupId>          <Artifactid>Ehcache</Artifactid>          <version>3.1.3</version>        </Dependency>        <!--JSR107 API -        <Dependency>          <groupId>Javax.cache</groupId>          <Artifactid>Cache-api</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-cache</Artifactid>        </Dependency>

The Ehcache 3.x configuration file is as follows:

<?XML version= "1.0" encoding= "UTF-8"?><ConfigXmlns:xsi= ' http://www.w3.org/2001/XMLSchema-instance 'xmlns:jsr107= ' http://www.ehcache.org/v3/jsr107 'xmlns= ' Http://www.ehcache.org/v3 'xsi:schemalocation= "Http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.1.xsd http://www.ehcache.org/v 3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.1.xsd ">  <!--<service> <jsr107:defaults> <jsr107:cache name= "City" template= "Heap-cache"/> </JSR1 07:defaults> </service> -  <cache-templatename= "Heap-cache">    <Resources>      <HeapUnit= "Entries">2000</Heap>       <OffheapUnit= "MB">100</Offheap>     </Resources>  </cache-template>  <Cachealias= "City"uses-template= "Heap-cache">    <expiry>      <TTLUnit= "Seconds">40</TTL>    </expiry>  </Cache> </Config>

The spring boot Application.properties is configured as follows:

#注意: The ehcache3.x configuration file path must be specified spring.cache.jcache.config=classpath:ehcache.xml

Use @enablecaching to open cache in spring boot
Finally, paste the spring cache annotation Example pseudo-code:

 PackageCom.lrh.service;Importjava.util.List;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.cache.annotation.CacheConfig;Importorg.springframework.cache.annotation.CacheEvict;ImportOrg.springframework.cache.annotation.CachePut;Importorg.springframework.cache.annotation.Cacheable;ImportOrg.springframework.stereotype.Service;Importorg.springframework.transaction.annotation.Transactional;ImportCom.github.pagehelper.PageHelper;ImportCom.github.pagehelper.PageInfo;ImportCom.lrh.dao.CityMapper;Importcom.lrh.domain.City;ImportCom.lrh.iservice.CityService, @Service @transactional@cacheconfig (Cachenames= "City")//@CacheDefaults (cachename = "City") Public classCityserviceimplImplementscityservice{@AutowiredPrivateCitymapper Citymapper; @Override @CachePut (Key= "#id")     PublicCity editcity (string ID, string name) {Citymapper.edit (ID, name); City City=NewCity ();        City.setid (long.valueof (id));        City.setname (name); returnCity ; } @Override PublicPageinfo<city>Selectcitybypage () {Pagehelper.startpage (1,5); List<City> list =Citymapper.selectall (); PageInfo<City> page =NewPageInfo (list); returnpage; }    /*** Condition The data that satisfies the cache condition is put into the cache, condition before and after calling the method will judge the * unless used to veto the cache update, unlike the condition, the expression only after the method execution judgment, at this time can get the return value Resul T is judged.*/@Override @Cacheable (Key= "#id", unless= "#result = = NULL")    //@CacheResult     PublicCity FindByID (String id) {returnCitymapper.selectcitybyid (ID); } @Override @CacheEvict (Key= "#id")     Public voidDelete (String id) {//citymapper.delete (ID);    }    /*** allentries Remove all*/@Override @CacheEvict (allentries=true)     Public voidDeleteAll () {citymapper.deleteall (); }}

Spring boot Spring Cache ehcache3.x Integration

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.