EHCache
是sourceforge
的开源缓存项目,现在已经具有独立的官网。其本身是纯Java实现的,所以和绝大多数的java项目无缝整合,例如Hibernate
的缓存就是基于EHCache
而实现的。
EHCache
支持内存和磁盘的缓存,默认是储存在内存当中,当内存不够是允许把缓存数据同步到磁盘当中,所以不需要担心内存不够的问题。
EHCache
直接在jvm虚拟机
中缓存,速度快,效率高;但缓存共享麻烦集群分布式应用不方便。
EHCache API
演示
1.引入依赖
<shiro-ehcache>1.4.2</shiro-ehcache>
<commons-io>2.6</commons-io>
<!-- 配置shiro整合ehcache的依赖-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>${shiro-ehcache}</version>
</dependency>
<!-- ehcache会在电脑存东西所以使用IO-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io}</version>
</dependency>
2.编写ehcache-shiro.xml
的配置参数
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="ehcache" upadteCheck="false">
<!-- 磁盘缓存位置-->
<diskStore path="java.io.tmpdir"/>
<!--
name:缓存名称。
maxElementsInMemory:缓存最大数目
maxElementsOnDisk:硬盘最大缓存个数。
eternal:对象是否永久有效,一但设置了,timeout将不起作用。
overflowToDisk:是否保存到磁盘,当系统当机时
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。
memoryStoreEvictionPolicy:
Ehcache的三种清空策略;
FIFO,first in first out,这个是大家最熟的,先进先出。
LFU, Less Frequently Used,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
LRU,Least Recently Used,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
-->
<!-- 默认缓存-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
<!-- 登录记录缓存锁定10分钟 -->
<cache name="loginRetryCache"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">
</cache>
</ehcache>
3.在自己的ShiroConfig
里配置shiro
整合ehcache
的CaCheManager
并在defaultWebSecurityManager
中设置。
/**
* 配置shiro整合ehcache的CaCheManager
*/
public EhCacheManager GetEhCacheManager() {
/**
* 1.创建shiro的cacheManager对象
*/
EhCacheManager cacheManager = new EhCacheManager();
/**
* 2.创建EhCache官方的cacheManager对象
* 获取EhCache的配置流对象
*/
InputStream is = null;
try {
is = ResourceUtils.getInputStreamForPath("classpath:ehcache/ehcache-shiro.xml");
} catch (IOException e) {
e.printStackTrace();
}
net.sf.ehcache.CacheManager cm = new net.sf.ehcache.CacheManager(is);
/**
* 3.将EhCache的cacheManager对象储存到shiro的cacheManager对象中
*/
cacheManager.setCacheManager(cm);
/**
* 4.返回
*/
return cacheManager;
}
@Bean
protected DefaultWebSecurityManager defaultWebSecurityManager() {
DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
/**
* 设置ehcache缓存
*/
defaultWebSecurityManager.setCacheManager(GetEhCacheManager());
return defaultWebSecurityManager;
}
4.代码完成开始测试。
测试成功,没加缓存时他会一直去访问授权的那个方法,我访问5次他只有第一次时去调用了授权方法(打印台的开始授权),剩下四次都是在内存当中直接取,没有访问我们的授权方法。
版权声明:本文为THanHan原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。