Shiro + EHCache 缓存的使用。

我是用 Maven  管理的项目,先上依赖包:

  1. <dependency>
  2. <artifactId>ehcache-core</artifactId>
  3. <groupId>net.sf.ehcache</groupId>
  4. <version>2.5.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.shiro</groupId>
  8. <artifactId>shiro-ehcache</artifactId>
  9. <version>1.2.2</version>
  10. </dependency>

 

我现在项目一直是用的  Shiro  Redis  来结合使用,解决的问题有

  1. 用户权限控制。
  2. 分布式部署 Session  共享。
  3. Cookie  管理。
  4. 缓存  管理。
  5. 用户信息、在线用户管理。
  6. ... ...

本文只介绍 Shiro  EHCache  的结合。因为我项目之前用的是 Redis  ,但是项目实际应用没那么多要求,就采用本地 缓存   EHCache  来解决了。

首先有两种方式来创建缓存实列:

一、 Spring  创建方式。

 
  1. <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
  2. p:configLocation="classpath:ehcache.xml"></bean>
  3.  
  4. <!-- 声明cacheManager -->
  5. <bean id="shiroEhcacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
  6. p:cacheManager-ref="cacheManagerFactory" ></bean>

二、Bean创建方式。

 
  1. <!-- shiro的缓存管理器,然后需要将缓存管理器注入到安全管理其中 -->
  2. <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
  3. <!--classpath是缓存属性的配置文件 -->
  4. <property name="cacheManagerConfigFile" value="classpath:config/ehcache-shiro.xml" />
  5. </bean>

我先暂且用第二种。

EHCache配置文件说明。

一、 EHCache 配置文件代码。

 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ehcache updateCheck="false" name="shirocache">
  3.  
  4. <diskStore path="java.io.tmpdir"/>
  5.  
  6. <!-- 登录记录缓存 锁定10分钟 -->
  7. <cache name="passwordRetryCache"
  8. maxEntriesLocalHeap="2000"
  9. eternal="false"
  10. timeToIdleSeconds="3600"
  11. timeToLiveSeconds="0"
  12. overflowToDisk="false"
  13. statistics="true">
  14. </cache>
  15.  
  16. <cache name="authorizationCache"
  17. maxEntriesLocalHeap="2000"
  18. eternal="false"
  19. timeToIdleSeconds="3600"
  20. timeToLiveSeconds="0"
  21. overflowToDisk="false"
  22. statistics="true">
  23. </cache>
  24.  
  25. <cache name="authenticationCache"
  26. maxEntriesLocalHeap="2000"
  27. eternal="false"
  28. timeToIdleSeconds="3600"
  29. timeToLiveSeconds="0"
  30. overflowToDisk="false"
  31. statistics="true">
  32. </cache>
  33.  
  34. <cache name="shiro-activeSessionCache"
  35. maxEntriesLocalHeap="2000"
  36. eternal="false"
  37. timeToIdleSeconds="3600"
  38. timeToLiveSeconds="0"
  39. overflowToDisk="false"
  40. statistics="true">
  41. </cache>
  42. <cache name="shiro_cache"
  43. maxElementsInMemory="2000"
  44. maxEntriesLocalHeap="2000"
  45. eternal="false"
  46. timeToIdleSeconds="0"
  47. timeToLiveSeconds="0"
  48. maxElementsOnDisk="0"
  49. overflowToDisk="true"
  50. memoryStoreEvictionPolicy="FIFO"
  51. statistics="true">
  52. </cache>
  53. </ehcache>

二、 EHCache  配置文件解释:

 name Cache的名称,必须是唯一的(ehcache会把这个cache放到HashMap里)
 maxElementsInMemory 内存中保持的对象数量
 maxElementsOnDisk DiskStore中保持的对象数量,默认值为0,表示不限制
 eternal 是否是永恒数据,如果是,则它的超时设置会被忽略
 overflowToDisk 如果内存中数据数量超过maxElementsInMemory限制,是否要缓存到磁盘上
 timeToIdleSeconds 对象空闲时间,指对象在多长时间没有被访问就会失效。只对eternal为false的有效。默认值0,表示一直可以访问
 timeToLiveSeconds 对象存活时间,指对象从创建到失效所需要的时间。只对eternal为false的有效。默认值0,表示一直可以访问
 diskPersistent 是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false
 diskExpiryThreadIntervalSeconds 对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次
 diskSpoolBufferSizeMB DiskStore使用的磁盘大小,默认值30MB。每个cache使用各自的DiskStore
 memoryStoreEvictionPolicy 如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU

三、 EHCache  管理类。

 
  1. package net.wenyifan.common.shiro;
  2.  
  3. import java.util.Collection;
  4. import java.util.HashSet;
  5. import java.util.Set;
  6.  
  7. import net.wenyifan.common.util.StringUtils;
  8.  
  9. import org.apache.shiro.cache.Cache;
  10. import org.apache.shiro.cache.CacheException;
  11. import org.apache.shiro.cache.ehcache.EhCacheManager;
  12. import org.apache.shiro.session.Session;
  13. /**
  14. *
  15. * 开发公司:itboy.net<br/>
  16. * 版权:itboy.net<br/>
  17. * <p>
  18. *
  19. * EHCache管理
  20. *
  21. * <p>
  22. *
  23. * 区分 责任人 日期    说明<br/>
  24. * 创建 周柏成 2016年2月16日  <br/>
  25. * <p>
  26. * *******
  27. * <p>
  28. * @author zhou-baicheng
  29. * @email i@itboy.net
  30. * @version 1.0,2016年2月16日 <br/>
  31. *
  32. */
  33. public class CacheManager<K, V> implements Cache<K, V> {
  34.  
  35. private EhCacheManager cacheManager;
  36.  
  37. private Cache<K, V> cache = null;
  38.  
  39. public Cache<K, V> getCache() {
  40. try {
  41. if(null == cache){
  42. cache = cacheManager.getCache("shiro_cache");
  43. }
  44. } catch (Exception e) {
  45. throw new RuntimeException(e);
  46. }
  47. return cache;
  48. }
  49.  
  50. @Override
  51. public void clear() throws CacheException {
  52. getCache().clear();
  53. }
  54.  
  55. @Override
  56. public V get(K key) throws CacheException {
  57. return getCache().get(key);
  58. }
  59.  
  60. @Override
  61. public Set<K> keys() {
  62.  
  63. return getCache().keys();
  64. }
  65.  
  66. @Override
  67. public V put(K key, V value) throws CacheException {
  68. return getCache().put(key, value);
  69. }
  70.  
  71. @Override
  72. public V remove(K key) throws CacheException {
  73. return getCache().remove(key);
  74. }
  75.  
  76. @Override
  77. public int size() {
  78. return getCache().size();
  79. }
  80.  
  81. @Override
  82. public Collection<V> values() {
  83. return getCache().values();
  84. }
  85.  
  86. public EhCacheManager getCacheManager() {
  87. return cacheManager;
  88. }
  89.  
  90. public void setCacheManager(EhCacheManager cacheManager) {
  91. this.cacheManager = cacheManager;
  92. }
  93.  
  94. public void setCache(Cache<K, V> cache) {
  95. this.cache = cache;
  96. }
  97.  
  98. /**
  99. * 获取所有Session
  100. * @throws Exception
  101. */
  102. public Collection<Session> AllSession() throws Exception {
  103. Set<Session> sessions = new HashSet<Session>();
  104. try {
  105. //TODO 注意事项:必须此缓存只存储Session,要不造成性能下降
  106. cache = getCache();
  107. Collection<V> values = cache.values();
  108. for (V v : values) {
  109. if(StringUtils.isNotBlank(v) && v instanceof Session){
  110. sessions.add((Session)v);
  111. }
  112. }
  113. } catch (Exception e) {
  114. throw e;
  115. }
  116. return sessions;
  117. }
  118. }

shiro  在使用 缓存  存储 Session  或者 存储其他的时候就可以用这个类了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值