在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();
}
}