EhCache是一个比较成熟的Java缓存框架,最先从hibernate发展而来, 是进程中的缓存系统,它提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案,快速简单。html
Springboot对ehcache的使用很是支持,因此在Springboot中只需作些配置就可以使用,且使用方式也简易。java
在你的项目上配置如下几步便可使用spring
pom.xml加依赖;缓存
<!-- Ehcache 坐标 --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency>
建立ehcache.xml配置文件app
位置:classpath目录下,即src/main/resources/ehcache.xml框架
内容:分布式
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <!--磁盘存储路径--> <diskStore path="java.io.tmpdir"/> <!--defaultCache:echcache的默认缓存策略 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </defaultCache> <!--accessToken缓存策略:自定义缓存策略--> <cache name="accessToken" maxElementsInMemory="10000" overflowToDisk="false" eternal="false" timeToIdleSeconds="1800" timeToLiveSeconds="0" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </cache> </ehcache>
说明:测试
一、<diskStore path=
"java.io.tmpdir"
/>
spa
磁盘存储路径,当内存缓存满了的时候,就会往这里面放,java.io.tmdir是操做系统缓存的临时目录,不一样操做系统缓存目录不同。操作系统
二、maxElementsInMemory:内存缓存中最多能够存放的元素数量,若放入Cache中的元素超过这个数值,则有如下两种状况:
1)若overflowToDisk=true,则会将Cache中多出的元素放入磁盘文件中;
2)若overflowToDisk=false,则根据memoryStoreEvictionPolicy策略替换Cache中原有的元素
三、overflowToDisk:内存不足时,是否启用磁盘缓存
四、eternal 缓存中对象是否永久有效
五、timeToIdleSeconds 缓存数据在失效前的容许闲置时间(单位:秒),仅当eternal=false时使用,默认值是0表示可闲置时间无穷大,若超过这个时间没有访问此Cache中的某个元素,那么此元素将被从Cache中清除
六、timeToLiveSeconds 缓存数据的总的存活时间(单位:秒),仅当eternal=false时使用,从建立开始计时,失效结束。
七、maxElementsOnDisk 磁盘缓存中最多能够存放的元素数量,0表示无穷大
八、diskExpiryThreadIntervalSeconds 磁盘缓存的清理线程运行间隔,默认是120秒
九、memoryStoreEvictionPolicy 内存存储与释放策略,即达到maxElementsInMemory限制时,Ehcache会根据指定策略清理内存 共有三种策略,分别为LRU(最近最少使用)、LFU(最经常使用的)、FIFO(先进先出)
下面说说他们之间的关系:
eternal很少说,true表示缓存永久有效,false表示不为永久有效
主要是timeToLiveSeconds 和timeToIdleSeconds 之间的使用(单独配置时,以上已说明)
举例说明1:timeToLiveSeconds =3600 timeToIdleSeconds =300
以上配置表明缓存有效时间为3600秒(自缓存创建起一个小时有效 ),在有效的一个小时内,若是连续五分钟未访问缓存,则缓存失效,特别说明的是,就算缓存访问从未间断,到一个小时后,缓存也会失效
举例说明2:timeToLiveSeconds =0 timeToIdleSeconds =300
以上配置表明缓存有效时间为永远有效,若是连续五分钟未访问缓存,则缓存失效。特别说明的是,若是缓存访问从未间断,该缓存会一直有效
另外,defaultCache是默认缓存方式,cache是自定义的缓存方式,自行设置name。这里设置为accessToken
在Springboot配置文件中把ehcache.xml配置进去;即在application.properties中加入如下配置代码
spring.cache.ehcache.config=ehcache.xml
第三步结束,ehcache在Springboot中就配置完成了,下面就是怎么在Springboot中使用。
在启动类前加上@EnableCaching注解;这样的话,启动类启动时会去启动缓存启动器。
@SpringBootApplication @EnableCaching public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } }
实现缓存方法
@Component public class TokenCache { public static final String CACHE_NAME = "accessToken"; //生成token存入缓存 @Cacheable(value = CACHE_NAME, key = "#token") public String setCache(String token) { System.out.println("set token:将token放入缓存"); return token; } //判断token是否存在缓存。不存在返回checkfail;存在返回token,并自动刷新闲置时间 @Cacheable(value = CACHE_NAME, key = "#token") public String checkToken(String token) { System.out.println("check fail:token验证失败"); return "checkfail"; } //清空token缓存 @CacheEvict(value = CACHE_NAME, allEntries=true) public void cleanAllToken() { System.out.println("clean all token:清空全部token"); } }
CACHE_NAME 为上面配置时的缓存规则名称,须要先声明一下
在方法setCache里面,作各类逻辑处理。缓存数据以kv形式存, 这里测试以value为key,以及value
第一次调用时候返回的值就会保存在缓存里。
这里为了更直观的体现出来,能够第一次调用setCache方法,把数据放到缓存里,第二次调用tokencheck方法,参数传上面的token值,返回的不是checkfail 而是setCache方法return的值
cleanAllToken:用于清空全部缓存。系统能够作个定时清空缓存,防止恶意访问致使的token占用
使用缓存方法
@Autowired private TokenCache tokenCache;//缓存方法类
新建一个token缓存
tokenCache.setCache(accessToken);
token缓存验证
String token = tokenCache.checkToken(accessToken.toString()); if(token.equals("checkfail")){ //过时:拦截并终止请求 }else{ //token正确,自动刷新闲置时间,转发请求 }
参考:https://blog.csdn.net/wuerwei1/article/details/80604142
https://www.cnblogs.com/xzmiyx/p/9897623.html