Ehcache整合spring配置 - 單機 入門

下面說明一下ehcache和spring整合配置。java

1.   須要的jar包spring

slf4j-api-1.6.1.jarapi

ehcache-core-2.1.0.jar緩存

ehcache-spring-annotations-1.1.2.jarapp

slf4j-log4j12-1.6.1.jarspa

spring-context-support-4.0.6.RELEASE.jar線程

2.   ehcache.xmlxml

<?xml version="1.0" encoding="UTF-8"?>對象

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

   

    <diskStore path="java.io.tmpdir/ehcache"/>

    

    <!-- 默認緩存 -->

    <defaultCache

  maxElementsInMemory="1000"

  eternal="false"

  timeToIdleSeconds="120"

  timeToLiveSeconds="120"

  overflowToDisk="false"/>

  

<!-- 菜單緩存 -->   

    <cache name="menuCache" 

           maxElementsInMemory="1000" 

           eternal="false"

  timeToIdleSeconds="120"

  timeToLiveSeconds="120"

  overflowToDisk="false" 

  memoryStoreEvictionPolicy="LRU"/>

    

</ehcache>

參數說明:

<diskStore>:當內存緩存中對象數量超過maxElementsInMemory時,將緩存對象寫到磁盤緩存中(需對象實現序列化接口)。

<diskStore path="">:用來配置磁盤緩存使用的物理路徑,Ehcache磁盤緩存使用的文件後綴名是*.data和*.index。

name:緩存名稱,cache的惟一標識(ehcache會把這個cache放到HashMap裏)。

maxElementsOnDisk:磁盤緩存中最多能夠存放的元素數量,0表示無窮大。

maxElementsInMemory:內存緩存中最多能夠存放的元素數量,若放入Cache中的元素超過這個數值,則有如下兩種狀況。

1)若overflowToDisk=true,則會將Cache中多出的元素放入磁盤文件中。

2)若overflowToDisk=false,則根據memoryStoreEvictionPolicy策略替換Cache中原有的元素。

Eternal:緩存中對象是否永久有效,便是否永駐內存,true時將忽略timeToIdleSeconds和timeToLiveSeconds。

timeToIdleSeconds:緩存數據在失效前的容許閒置時間(單位:秒),僅當eternal=false時使用,默認值是0表示可閒置時間無窮大,此爲可選屬性即訪問這個cache中元素的最大間隔時間,若超過這個時間沒有訪問此Cache中的某個元素,那麼此元素將被從Cache中清除。

timeToLiveSeconds:緩存數據在失效前的容許存活時間(單位:秒),僅當eternal=false時使用,默認值是0表示可存活時間無窮大,即Cache中的某元素從建立到清楚的生存時間,也就是說從建立開始計時,當超過這個時間時,此元素將從Cache中清除。

overflowToDisk:內存不足時,是否啓用磁盤緩存(即內存中對象數量達到maxElementsInMemory時,Ehcache會將對象寫到磁盤中),會根據標籤中path值查找對應的屬性值,寫入磁盤的文件會放在path文件夾下,文件的名稱是cache的名稱,後綴名是data。

diskPersistent:是否持久化磁盤緩存,當這個屬性的值爲true時,系統在初始化時會在磁盤中查 找文件名爲cache名稱,後綴名爲index的文件,這個文件中存放了已經持久化在磁盤中的cache的index,找到後會把cache加載到內存, 要想把cache真正持久化到磁盤,寫程序時注意執行net.sf.ehcache.Cache.put(Element element)後要調用flush()方法。

diskExpiryThreadIntervalSeconds:磁盤緩存的清理線程運行間隔,默認是120秒。

diskSpoolBufferSizeMB:設置DiskStore(磁盤緩存)的緩存區大小,默認是30MB

memoryStoreEvictionPolicy:內存存儲與釋放策略,即達到maxElementsInMemory限制時,Ehcache會根據指定策略清理內存,共有三種策略,分別爲LRU(最近最少使用)、LFU(最經常使用的)、FIFO(先進先出)。

3.   application_spring_cache.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:context="http://www.springframework.org/schema/context" 

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

    xsi:schemaLocation="http://www.springframework.org/schema/beans 

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

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

        http://www.springframework.org/schema/context/spring-context-3.0.xsd

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

        http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">

    

    <cache:annotation-driven cache-manager="cacheManager"/>

    

     <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

        <property name="configLocation" value="classpath:application/ehcache.xml" />

    </bean>

    

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">    

        <property name="cacheManager"  ref="cacheManagerFactory"/>    

    </bean>

 

</beans>

4.   使用

首先在ehcache.xml中配置緩存策略,即添加一組cache。

注意事項:

 <cache:annotation-driven/>還能夠指定一個mode屬性,可選值有proxyaspectj。默認是使用proxymodeproxy時,只有緩存方法在外部被調用的時候Spring Cache纔會發生做用,也就是當一個支持緩存的方法在對象內部被調用時是不會觸發緩存功能的。

使用proxy時,只有public方法上的@Cacheable等標註纔會起做用,若是須要非public方法上的方法也能夠使用Spring Cache時把mode設置爲aspectj

4.1     添加緩存

@Cacheable(value="menuCache",key="'UserMenuKey'+#userid")

    public List<MenuBean> queryMenuByUserId(String userid)

{

……

}

@Cacheable主要針對方法配置,可以根據方法的請求參數對其結果進行緩存。

相關文章
相關標籤/搜索