目标:让Shiro整合ehcache,提供缓存realm数据的功能。
1.引入encache配置文件,配置缓存
1 <!-- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> 2 --><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> 3 4 <!-- 磁盘上的缓存的临时目录 ,默认是系统的临时目录,也可以手动指定一个目录--> 5 <diskStore path="java.io.tmpdir"/> 6 <!-- 默认的缓存区域的默认策略 --> 7 <!-- 8 maxElementsInMemory:内存中元素最大存放的个数 9 eternal:缓存的对象是否永生不死。一般都是false。 10 timeToIdleSeconds:发呆的时间,多长时间不用,就干掉,秒 11 timeToLiveSeconds:存活的时间,活够了就干掉,秒 12 maxElementsOnDisk:硬盘上最大存放的元素的个数,如果内存10000个满了,就往硬盘上存。 13 memoryStoreEvictionPolicy:内存中存储和销毁元素的策略:默认使用LRU,解决的是缓存元素满了怎么办。 14 策略有三个:LRU、LFU、FIFO 15 LRU:最少使用被清理,次数 16 LFU:时间,闲置最长的时间 17 FIFO:管道策略,先进先出 18 --> 19 <defaultCache 20 maxElementsInMemory="10000" 21 eternal="false" 22 timeToIdleSeconds="120" 23 timeToLiveSeconds="120" 24 maxElementsOnDisk="10000000" 25 diskExpiryThreadIntervalSeconds="120" 26 memoryStoreEvictionPolicy="LRU"> 27 <persistence strategy="localTempSwap"/> 28 </defaultCache> 29 <!-- Spring整合的菜单缓存 --> 30 <cache name="bos_menu_cache" 31 maxElementsInMemory="10000" 32 eternal="false" 33 timeToIdleSeconds="120" 34 timeToLiveSeconds="120" 35 maxElementsOnDisk="10000000" 36 diskExpiryThreadIntervalSeconds="120" 37 memoryStoreEvictionPolicy="LRU"> 38 <persistence strategy="localTempSwap"/> 39 </cache> 40 <!-- Shiro权限缓存-认证 --> 41 <cache name="bos_realm_authentication_cache" 42 maxElementsInMemory="10000" 43 eternal="false" 44 timeToIdleSeconds="120" 45 timeToLiveSeconds="120" 46 maxElementsOnDisk="10000000" 47 diskExpiryThreadIntervalSeconds="120" 48 memoryStoreEvictionPolicy="LRU"> 49 <persistence strategy="localTempSwap"/> 50 </cache> 51 <!-- Shiro权限缓存-授权 --> 52 <cache name="bos_realm_authorization_cache" 53 maxElementsInMemory="10000" 54 eternal="false" 55 timeToIdleSeconds="120" 56 timeToLiveSeconds="120" 57 maxElementsOnDisk="10000000" 58 diskExpiryThreadIntervalSeconds="120" 59 memoryStoreEvictionPolicy="LRU"> 60 <persistence strategy="localTempSwap"/> 61 </cache> 62 </ehcache>