Ehcache緩存配置和基本使用
阿新 • • 發佈:2018-03-27
app www tor 持久 simple 開源 end mave 統計
關於這點的特性,官方給了一個很可愛的名字small foot print ,一般Ehcache的發布版本不會到2M,V 2.2.3 才 668KB。
4、夠輕量
核心程序僅僅依賴slf4j這一個包,沒有之一!
5、好擴展
Ehcache提供了對大數據的內存和硬盤的存儲,最近版本允許多實例、保存對象高靈活性、提供LRU、LFU、FIFO淘汰算法,基礎屬性支持熱配置、支持的插件多
6、監聽器
緩存管理器監聽器 (CacheManagerListener)和 緩存監聽器(CacheEvenListener),做一些統計或數據一致性廣播挺好用的
前言
在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 }
轉載http://www.zhoutao123.com/?p=106
Ehcache緩存配置和基本使用