SpringMVC集成緩存框架Ehcache


  

  在互聯網應用中,應用並發比傳統企業及應用會高出很多。解決並發的根本在於系統的響應時間與單位時間的吞吐量。思路可分為:一減少系統的不必要開支(如緩存),二是提高系統單位時間內的運算效率(如集群)。 在硬件資源一定的情況下,在軟件層面上解決高並發會比較經濟實惠一些。緩存又分為客戶端緩存(web瀏覽器)與服務器緩存;常用的比較流行的服務器緩存框架如Ehcache。下面針對最近學習的Ehcache緩存做一下介紹。

一、ehcache需要引入包

<!--ehcache 相關包 -->
<dependency>
    <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
    <version>2.7.5</version>
</dependency>
<dependency>
    <groupId>com.googlecode.ehcache-spring-annotations</groupId>
    <artifactId>ehcache-spring-annotations</artifactId>
    <version>1.2.0</version>
</dependency>

 

二、配置applicationContext-ehcache.xml和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" xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
    
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"/>
    </bean>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="cacheManagerFactory"/>
    </bean>
    
    <!-- 開啟spring緩存 -->
    <cache:annotation-driven cache-manager="cacheManager" />

</beans>
ehcache配置文件

必須導入導入命名空間

xmlns:cache="http://www.springframework.org/schema/cache"

http://www.springframework.org/schema/cache

http://www.springframework.org/schema/cache/spring-cache-4.1.xsd

以及ehcache緩存管理器及開啟;

<?xml version="1.0" encoding="UTF-8"?>
<!-- updateCheck:是否檢查當前使用的Ehcache的版本 -->
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
    monitoring="autodetect" dynamicConfig="true">

    <!-- 緩存到磁盤路徑 -->
    <diskStore path="d:/cache" />
    <!--   
    eternal:緩存中對象是否為永久的,如果是,超時設置將被忽略,對象從不過期
    maxElementsInMemory:緩存中允許創建的最大對象數
    timeToIdleSeconds:緩存數據的鈍化時間,也就是在一個元素消亡之前,兩次訪問時間的最大時間間隔值,這只能在元素不是永久駐留時有效,如果該值是 0 就意味着元素可以停頓無窮長的時間。
    timeToLiveSeconds:緩存數據的生存時間,也就是一個元素從構建到消亡的最大時間間隔值,這只能在元素不是永久駐留時有效,如果該值是0就意味着元素可以停頓無窮長的時間。
    memoryStoreEvictionPolicy:緩存滿了之后的淘汰算法。
        1 FIFO,先進先出
        2 LFU,最少被使用,緩存的元素有一個hit屬性,hit值最小的將會被清出緩存。
        3 LRU,最近最少使用的,緩存的元素有一個時間戳,當緩存容量滿了,而又需要騰出地方來緩存新的元素的時候,那么現有緩存元素中時間戳離當前時間最遠的元素將被清出緩存。
    -->
    
    <!-- 默認緩存 -->
    <defaultCache maxElementsInMemory="10000" eternal="false"
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
        maxElementsOnDisk="10000000" diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
    
    <!-- 自定義緩存 -->
    <cache name="baseCache" maxElementsInMemory="200"
        maxElementsOnDisk="1000" eternal="false" overflowToDisk="true"
        diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600"
        memoryStoreEvictionPolicy="LFU" />
</ehcache>
ehcache.xml

一般使用默認的緩存defaultCache,根據緩存的配置不同,可自定義緩存如文件中的baseCache。

 

三、ehcache緩存的使用

1、使用@Cacheable注解類緩存數據

  @Cacheable注解可以用在方法或者類級別,用在方法上,僅僅緩存此方法的返回值; 用在類上,針對該類所有的方法的返回值緩存。下面展示方法級別緩存,,

  當緩存中沒有該對象的時候,當然要從數據庫里面訪問了,從數據庫查出來之后,緩存管理器會將此對象放到緩存中,下一次訪問的時候,只要該對象沒有消亡則會從緩存里取,不會再查詢數據庫。

  @Cacheable注解包含三個參數 @Cacheable(value,key,condition);  

  value:我們自定義緩存的name,將被緩存的位置;

  key :任何存儲在緩存中的數據為了高速訪問都需要一個key。spring默認使用被@Cacheable注解的方法的簽名來作為key,當然你可以重寫key,自定義key可以使用SpEL表達式。key的值可是使用 key = "#page" SpEl表達式,也可以使用 'String' 字符形式。

  condition:也使用SpEl表達式,用條件控制是否緩存。

 

 2、@CacheEvict 這個注解的作用就是當數據發生變化的時候(增刪改)清除緩存,做到數據同步

 參數value對應的默認緩存或者自定義緩存;key 對應哪個緩存

 

 


免責聲明!

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



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