Ehcache快取配置和基本使用
阿新 • • 發佈:2019-02-16
前言
在java專案廣泛的使用中。它是一個開源的、設計於提高在資料從RDBMS中取出來的高花費、高延遲採取的一種快取方案。
正因為Ehcache具有健壯性(基於java開發)、被認證(具有apache 2.0 license)、充滿特色(稍後會詳細介紹),
所以被用於大型複雜分散式web application的各個節點中。
特點
1、 夠快
Ehcache的發行有一段時長了,經過幾年的努力和不計其數的效能測試,Ehcache終被設計於large, high concurrency systems.
2、夠簡單
開發者提供的介面非常簡單明瞭,從Ehcache的搭建到運用執行僅僅需要的是你寶貴的幾分鐘。其實很多開發者都不知道自己用在用Ehcache,Ehcache被廣泛的運用於其他的開源專案
比如:hibernate
3、 夠袖珍
關於這點的特性,官方給了一個很可愛的名字small foot print ,一般Ehcache的釋出版本不會到2M,V 2.2.3 才 668KB。
4、夠輕量
核心程式僅僅依賴slf4j這一個包,沒有之一!
5、好擴充套件
Ehcache提供了對大資料的記憶體和硬碟的儲存,最近版本允許多例項、儲存物件高靈活性、提供LRU、LFU、FIFO淘汰演算法,基礎屬性支援熱配置、支援的外掛多
6、監聽器
快取管理器監聽器 (CacheManagerListener)和 快取監聽器(CacheEvenListener),做一些統計或資料一致性廣播挺好用的
如何使用
Maven依賴
1 <!--加入快取--> 2<dependency> 3 <groupId>net.sf.ehcache</groupId> 4 <artifactId>ehcache-core</artifactId> 5 <version>2.6.6</version> 6 </dependency>
配置檔案
在resources資源目錄下建立一個ehcache-config.xml檔案,內容如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" 4 updateCheck="false"> 5 <!-- EhCache在每次啟動的時候都要連線到 ehcache 網站上去檢查新版本 使用如上的 updateCheck="false" 來禁止這個檢查新版本 --> 6 7 <!-- 8 name:cache唯一標識 9 eternal:快取是否永久有效 10 maxElementsInMemory:記憶體中最大快取物件數 11 overflowToDisk(true,false):快取物件達到最大數後,將快取寫到硬碟中 12 diskPersistent:硬碟持久化 13 timeToIdleSeconds:快取清除時間 14 timeToLiveSeconds:快取存活時間 15 diskExpiryThreadIntervalSeconds:磁碟快取的清理執行緒執行間隔 16 memoryStoreEvictionPolicy:快取清空策略 17 1.FIFO:first in first out 先進先出 18 2.LFU: Less Frequently Used 一直以來最少被使用的 19 3.LRU:Least Recently Used 最近最少使用的 20 --> 21 22 <diskStore path="java.io.tmpdir/ehcache" /> 23 24 <defaultCache 25 maxElementsInMemory="10000" 26 eternal="false" 27 timeToIdleSeconds="120" 28 timeToLiveSeconds="120" 29 overflowToDisk="true" 30 maxElementsOnDisk="10000000" 31 diskPersistent="false" 32 diskExpiryThreadIntervalSeconds="120" 33 memoryStoreEvictionPolicy="FIFO" /> 34 35 <cache name="normal_cache" 36 maxElementsInMemory="200" 37 eternal="false" 38 timeToIdleSeconds="7200" 39 timeToLiveSeconds="7200" 40 overflowToDisk="true" 41 maxElementsOnDisk="1000" 42 diskPersistent="false" 43 diskExpiryThreadIntervalSeconds="120" 44 memoryStoreEvictionPolicy="FIFO" /> 45 </ehcache>
spring整合配置
注意以下內容必須註冊在spring的主配置檔案中
1 <!--快取配置檔案介面--> 2 <cache:annotation-driven cache-manager="cacheManager"/> 3 <!--建立快取管理器工廠--> 4 <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> 5 <property name="configLocation" value="classpath:ehcache-config.xml"></property> 6 </bean> 7 <!--建立快取管理器--> 8 <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> 9 <property name="cacheManager" ref="cacheManagerFactory"></property> 10 </bean>
使用方法
這裡可以使用註解的方式 @Cacheable(value = “cache_pos_codes”) 其中value的是設定的配置檔案ehcache-config.xml的配置名稱,需要注意的是import org.springframework.cache.annotation.Cacheable;
1 @RequestMapping(value = "/date",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE + CHARSET) 2 @ResponseBody 3 @Cacheable(value = "cache_pos_codes") 4 public String getDate(){ 5 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 6 return simpleDateFormat.format(new Date()); 7 }