在pom中引入相应的jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.8.3</version>
</dependency>
在resources目录下面创建ehcache.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<!-- //对象是否永久有效,一但设置了,timeout将不起作用。-->
<!-- eternal="false"-->
<!-- //内存缓存最大数目-->
<!-- maxElementsInMemory="1000"-->
<!-- //硬盘最大缓存个数-->
<!-- maxElementsOnDisk="5000"-->
<!-- //当数量达到maxElementsInMemory,Ehcache将会对象写到磁盘中-->
<!-- overflowToDisk="false"-->
<!-- //是否缓存虚拟机重启期数据-->
<!-- diskPersistent="false"-->
<!-- 缓存自失效前允许闲置的时间-->
<!-- timeToIdleSeconds="0"-->
<!-- 缓存自失效前允许存货的时间-->
<!-- timeToLiveSeconds="600"-->
<!-- //当达到缓存最大值限制时,缓存清除策略-->
<!-- memoryStoreEvictionPolicy="LRU"-->
<!-- memoryStoreEvictionPolicy:可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。 -->
<diskStore path="java.io.tmpdir" />
<defaultCache
eternal="false"
maxElementsInMemory="10000"
maxElementsOnDisk="120"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
memoryStoreEvictionPolicy="LRU" />
<!-- 缓存名称demo -->
<cache
name="demo"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskExpiryThreadIntervalSeconds="60"
maxElementsInMemory="50"
overflowToDisk="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
在application.properties文件中,通过配置来指定EhCache配置文件的位置
spring.cache.jcache.config=classpath:ehcache.xml
在启动类上添加@EnableCaching开启缓存
@EnableCaching
创建serverImpl,注意只能在实现类方法上添加,不能在接口的方法上。
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
/**
* 创 建 人 : wangsheng 创建日期:2019年12月
*/
@Service
public class InfoServiceImpl {
//value属性表示使用哪个缓存策略,缓存策略在ehcache.xml中
//LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)
public static final String DEMO_CACHE_NAME = "demo";//这个demo和ehcache中的 name对应
//所有key 中的单引号 都不能少,不然会被识别成一个对象
@Cacheable(value = DEMO_CACHE_NAME, key = "'info_'+#id")
public String findNameById(Integer id) {
//Cacheable 不存在这个key 才会进入方法
System.out.println("没有走缓存:" + id);
return "wangsheng";
}
@CacheEvict(value = DEMO_CACHE_NAME, key = "'info_'+#id")//清除缓存:是根据这个key来清理的,没有找到key对应的缓存就没清理了
public void delete(Integer id) {
//数据库删除操作……(略)
}
@CachePut(value = DEMO_CACHE_NAME, key = "'info_'+#id")
public void update(Integer id) {
/*
@cachePut 表示重新放入缓存对象,不管是否存在这个key的缓存,所以一定会进入方法
*/
}
}
创建测试类
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.beans.factory.annotation.Autowired;
@RunWith(SpringRunner.class)
@SpringBootTest
public class controller {
@Autowired
InfoServiceImpl infoService;
@Test
public void dd(){
for(int i=0;i<10;i++){
String cc=infoService.findNameById(1);
System.out.println(cc);
}
}
}
可以看到只有第一次进了方法,其余几次都走了缓存。