spring+shiro+ehcache整合


1.導入jar包(pom.xml文件)

                 <!-- ehcache緩存框架 -->
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache-core</artifactId>
			<version>2.6.11</version>
		</dependency>    

  Spring 整合 ehcache 包 spring-context-support 包

2.使用 ehcache ,導入 ehcache.xml 配置文件

解壓 ehcache-core.jar 包 ,將 ehcache-failsafe.xml 復制 src/main/resources
改名 ehcache.xml

 

ehcache.xml文件

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">

    <diskStore path="java.io.tmpdir"/>
	<!-- 默認緩存區 -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>
    <!-- 自定義緩存區 -->
    <cache name="bos"
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </cache>
    <!-- 自定義緩存區 -->
    <cache name="standard"
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </cache>
    
</ehcache>

 

diskStore

指定數據存儲位置,可指定磁盤中的文件夾位置

defaultCache

默認的管理策略

name

Cache的名稱,必須是唯一的(ehcache會把這個cache放到HashMap里)

maxElementsInMemory

在內存中緩存的element的最大數目

如果放入cache中的元素超過這個數值,有兩種情況:

1、若overflowToDisk的屬性值為true,會將cache中多出的元素放入磁盤文件中。

2、若overflowToDisk的屬性值為false,會根據memoryStoreEvictionPolicy的策略替換cache中原有的元素。

eternal

設定緩存的elements是否永遠不過期。
如果為true,則緩存的數據始終有效,
如果為false那么還要根據timeToIdleSeconds,timeToLiveSeconds判斷。

overflowToDisk

如果內存中數據超過內存限制,是否要緩存到磁盤上。

maxElementsOnDisk

在磁盤上緩存的element的最大數目,默認值為0,表示不限制。

timeToIdleSeconds

對象空閑時間,指對象在多長時間沒有被訪問就會失效。只對eternal為false的有效。默認值0,表示一直可以訪問。以秒為單位。

timeToLiveSeconds

對象存活時間,指對象從創建到失效所需要的時間。只對eternal為false的有效。默認值0,表示一直可以訪問。以秒為單位。 

diskPersistent

是否在磁盤上持久化。指重啟jvm后,數據是否有效。默認為false。 

diskExpiryThreadIntervalSeconds

對象檢測線程運行時間間隔。標識對象狀態的線程多長時間運行一次。以秒為單位。

iskSpoolBufferSizeMB

DiskStore使用的磁盤大小,默認值30MB。每個cache使用各自的DiskStore。

memoryStoreEvictionPolicy

如果內存中數據超過內存限制,向磁盤緩存時的策略。默認值LRU,可選FIFO、LFU。

緩存的3 種清空策略 :

FIFO ,first in first out (先進先出).

LFU , Less Frequently Used (最少使用).意思是一直以來最少被使用的。緩存的元素有一個hit 屬性,hit 值最小的將會被清出緩存。 

LRU ,Least Recently Used(最近最少使用). 
(ehcache 默認值).緩存的元素有一個時間戳,當緩存容量滿了,
而又需要騰出地方來緩存新的元素的時候,那么現有緩存元素中時間戳離當前時間最遠的元素將被清出緩存。

 

3.applicationContext-ehcache.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/cache 
		http://www.springframework.org/schema/cache/spring-cache.xsd ">
 
	 <!-- 緩存配置  -->
	<bean id="ehCacheManager" 
		class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
		<property name="configLocation" value="classpath:ehcache.xml" />
	</bean>
	
	<!-- shiro封裝cacheManager -->
	<bean id="shiroCacheManager" 
		class="org.apache.shiro.cache.ehcache.EhCacheManager">
		<property name="cacheManager" ref="ehCacheManager" />
	</bean>
	
	<!-- spring 封裝ehcache緩存管理器  -->
	<bean id="springCacheManager" 
		class="org.springframework.cache.ehcache.EhCacheCacheManager">
		<property name="cacheManager" ref="ehCacheManager" />
	</bean>
	
	<!-- 激活spring 緩存注解 -->
	<cache:annotation-driven cache-manager="springCacheManager"/>
	
</beans>

  加載該配置文件

4.修改web.xml文件

<!-- spring配置文件位置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext*.xml</param-value>
	</context-param>

 5.將cache管理器注入到安全管理器中 

6.對認證數據、授權數據 哪些進行緩存 ?

對應到ehcache.xml文件中的自定義的緩存緩存區

注意: 使需要緩存對象,實現 Serializable 接口

使用注解進行開發

第七步: 在被 spring 管理 bean 對象方法上 使用@Cacheable 、@CacheEvict
@Cacheable 應用緩存區,對方法返回結果進行緩存 ---- 用於查詢方法
@CacheEvict 清除緩存區數據 --- 用於 增加、修改、刪除 方法

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM