Ehcache——基於註解的緩衝使用

來源:互聯網
上載者:User

標籤:

  ehcache是一個非常輕量級的緩衝實現,而且從1.2之後就支援了叢集,而且是hibernate預設的緩衝provider。EhCache 是一個純Java的進程內緩衝架構,具有快速、精乾等特點,是Hibernate中預設的CacheProvider。

  Ehcache的分布式緩衝有傳統的RMI,1.5版的JGroups,1.6版的JMS。分布式緩衝主要解決叢集環境中不同的伺服器間的資料的同步問題。

使用Spring的AOP進行整合,可以靈活的對方法的返回結果對象進行緩衝。

CachingFilter功能可以對HTTP響應的內容進行緩衝。

1、主要特性
     1) 快速.
     2) 簡單.
     3) 多種緩衝策略
     4) 快取資料有兩級:記憶體和磁碟,因此無需擔心容量問題
     5) 快取資料會在虛擬機器重啟的過程中寫入磁碟
     6) 可以通過RMI、可插入API等方式進行分布式緩衝
     7) 具有緩衝和緩衝管理器的偵聽介面
     8) 支援多緩衝管理器執行個體,以及一個執行個體的多個快取區域
     9) 提供Hibernate的緩衝實現
     10) 等等

2、 使用ehcache時,需要在spring的配置中添加如下配置:

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

<!--1)cache的命名空間;--><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
   <!--2)啟用緩衝註解功能,否則就無法通過註解進行緩衝;-->
    <cache:annotation-driven cache-manager="cacheManager"/>

  <!--3)下面的配置使用了一個Spring提供的EhCacheCacheManager來產生一個Spring的CacheManager,其接收一個Ehcache的CacheManager,因為真正用來存入緩衝數  據的還是Ehcache。Ehcache的CacheManager是通過Spring提供的EhCacheManagerFactoryBean來產生的,可以通過指定ehcache的設定檔位置來產生一個Ehcache的Ca  cheManager。若未指定則將按照Ehcache的預設規則取classpath根路徑下的ehcache.xml檔案;若該檔案也不存在,則擷取Ehcache對應jar包中的ehcache-failsafe.xm  l檔案作為設定檔。-->
    <bean id="ehCacheManagerFactory" 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="ehCacheManagerFactory"/>        <!-- 表示是否事務環繞的,如果true,則如果交易回復,緩衝也復原,預設false -->        <property name="transactionAware" value="true"/>    </bean></beans>

  上面的配置使用了一個Spring提供的EhCacheCacheManager來產生一個Spring的CacheManager,其接收一個Ehcache的CacheManager,因為真正用來存入快取資料的還是Ehcache。Ehcache的CacheManager是通過Spring提供的EhCacheManagerFactoryBean來產生的,其可以通過指定ehcache的設定檔位置來產生一個Ehcache的CacheManager。若未指定則將按照Ehcache的預設規則取classpath根路徑下的ehcache.xml檔案,若該檔案也不存在,則擷取Ehcache對應jar包中的ehcache-failsafe.xml檔案作為設定檔。更多關於Ehcache的內容這裡就不多說了,它不屬於本文討論的內容,欲瞭解更多關於Ehcache的內容可以參考我之前發布的Ehcache系列文章,也可以參考官方文檔等。

 

3、ehcache.xml配置格式說明:

diskStore:

指定一個檔案目錄,當EHCache把資料寫到硬碟上時,將把資料寫到這個檔案目錄下;

取值有:

user.home – 使用者主目錄

user.dir      – 使用者當前工作目錄

java.io.tmpdir – 預設臨時檔案路徑

defaultCache:

設定緩衝的預設資料到期策略

Cache:

 設定具體的具名快取的資料到期策略;

Cache中包含的屬性主要有:

maxElementsInMemory:可以緩衝元素最大個數;

Eternal:true表示對象永不到期,此時會忽略timeToIdleSeconds和timeToLiveSeconds屬性,預設為false  

overflowToDisk:true,表示當快取資料達到maxElementsInMemory時,會把溢出的對象緩衝到磁碟中;false,會根據到期策略進行淘汰;

timeToIdleSeconds:對象閒置最長時間,以秒為單位,達到這個這時間時對象會到期;

timeToLiveSeconds:設定對象允許存在於緩衝中的最長時間,以秒為單位;

diskPersistent:否在磁碟上持久化。指重啟jvm後,資料是否有效。

<?xml version="1.0" encoding="UTF-8"?><ehcache>    <diskStore path="java.io.tmpdir"/>    <defaultCache        maxElementsInMemory="10000"        eternal="false"        overflowToDisk="false"        timeToIdleSeconds="1800"        timeToLiveSeconds="1800"        diskPersistent="false"/>    <cache name="bdRules"           maxElementsInMemory="200"           eternal="false"           overflowToDisk="false"           timeToIdleSeconds="60"           timeToLiveSeconds="60"           memoryStoreEvictionPolicy="LFU" />    <cache name="bdRulesCount"           maxElementsInMemory="200"           eternal="false"           overflowToDisk="false"           timeToIdleSeconds="60"           timeToLiveSeconds="60"           memoryStoreEvictionPolicy="LFU" />    <cache name="bdmRules"           maxElementsInMemory="20"           eternal="false"           overflowToDisk="false"           timeToIdleSeconds="60"           timeToLiveSeconds="60"           memoryStoreEvictionPolicy="LFU" /> </ehcache>

4、註解介紹:

 

1)Cacheable:添加緩衝;如果已經緩衝,則不執行方法,直接返回緩衝結果

value:緩衝名稱,不為空白,與ehcache.xml中聲明的cache的name一致

key:緩衝的key,預設為空白,使用方法的參數類型及參數值作為key,支援SpEL

condition:觸發條件,當條件滿足時會將方法的傳回值緩衝,預設為空白,既表示全部都加入緩衝,支援SpEL

@Cacheable(value = "bdmRules", key = "#p0")SaleCommissionRuleBdmEntity queryBdmRuleByMonth(@Param("timeInterval") String timeInterval, @Param("department") String department);

2)CacheEvict:執行方法,並且清除緩衝

value:緩衝位置名稱,不可為空

key:緩衝的key,預設為空白,同上

condition:觸發條件,只有滿足條件的情況才會清除緩衝,預設為空白,支援SpEL

allEntries:true表示清除value中的全部緩衝,預設為false

3)CachePut:既要保證方法被執行,又緩衝結果

參數同Cacheable

 

Ehcache——基於註解的緩衝使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.