http://blog.csdn.net/qq18998401056/article/details/53467671
**************************************************************************
在Spring Boot中通過@EnableCaching注解自動化配置合適的緩存管理器(CacheManager),Spring Boot根據下面的順序去偵測緩存提供者:
Generic
JCache (JSR-107)
EhCache 2.x
Hazelcast
Infinispan
Redis
Guava
Simple
除了按順序偵測外,我們也可以通過配置屬性spring.cache.type來強制指定。默認是simple類型。
由於ehcache3.x實現了jsr-107的標准接口,而本文通過整合ehcache3.x來使用JCache的方式。
引入依賴如下:
<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>
ehcache 3.x配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <config xmlns: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/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.1.xsd"> <!-- <service> <jsr107:defaults> <jsr107:cache name="city" template="heap-cache"/> </jsr107:defaults> </service> --> <cache-template name="heap-cache"> <resources> <heap unit="entries">2000</heap> <offheap unit="MB">100</offheap> </resources> </cache-template> <cache alias="city" uses-template="heap-cache"> <expiry> <ttl unit="seconds">40</ttl> </expiry> </cache> </config>
spring boot application.properties配置如下:
#注意:ehcache3.x配置文件路徑必須指定 spring.cache.jcache.config=classpath:ehcache.xml
在spring boot 使用@EnableCaching 開啟緩存
最后,貼出spring cache注解例子偽代碼:
package com.lrh.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.lrh.dao.CityMapper; import com.lrh.domain.City; import com.lrh.iservice.CityService; @Service @Transactional @CacheConfig(cacheNames = "city") //@CacheDefaults(cacheName = "city") public class CityServiceImpl implements CityService{ @Autowired private CityMapper cityMapper; @Override @CachePut(key = "#id") public City editCity(String id, String name) { cityMapper.edit(id, name); City city=new City(); city.setId(Long.valueOf(id)); city.setName(name); return city; } @Override public PageInfo<City> selectCityByPage() { PageHelper.startPage(1,5); List<City> list = cityMapper.selectAll(); PageInfo<City> page = new PageInfo(list); return page; } /** * condition滿足緩存條件的數據才會放入緩存,condition在調用方法之前和之后都會判斷 * unless用於否決緩存更新的,不像condition,該表達只在方法執行之后判斷,此時可以拿到返回值result進行判斷了 */ @Override @Cacheable(key = "#id",unless="#result == null") //@CacheResult public City findById(String id) { return cityMapper.selectCityById(id); } @Override @CacheEvict(key="#id") public void delete(String id) { //cityMapper.delete(id); } /** * allEntries移除所有 */ @Override @CacheEvict(allEntries = true) public void deleteAll() { cityMapper.deleteAll(); } }