Ehcache 的簡單實用 及配置,ehcache簡單實用
Ehcache 與 spring 整合後的用法,下面是一個Ehcache.xml設定檔;
通用的緩衝策略 可以用一個 cache;
1 <?xml version="1.0" encoding="UTF-8"?> 2 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" 4 updateCheck="false"> 5 6 <!-- 7 diskStore:為緩衝路徑,ehcache分為記憶體和磁碟 2級,此屬性定義磁碟的緩衝位置 8 user.home - 使用者主目錄 9 user.dir - 使用者當前工作目錄10 java.io.tmpdir - 預設臨時檔案路徑11 -->12 <diskStore path="java.io.tmpdir/Tmp_Ehcache" />13 <!--14 name:緩衝名稱。15 maxElementsInMemory:緩衝最大數目16 maxElementsOnDisk:硬碟最大緩衝個數。17 eternal:對象是否永久有效,一但設定了,timeout將不起作用。18 overflowToDisk:是否儲存到磁碟,當系統當機時19 timeToIdleSeconds:設定對象在失效前的允許閑置時間(單位:秒)。僅當eternal=false對象不是永久有效時使用,可選屬性,預設值是0,也就是可閑置時間無窮大。20 timeToLiveSeconds:設定對象在失效前允許存活時間(單位:秒)。最大時間介於建立時間和失效時間之間。僅當eternal=false對象不是永久有效時使用,預設是0.,也就是對象存活時間無窮大。21 diskPersistent:是否緩衝虛擬機器重啟期資料 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.22 diskSpoolBufferSizeMB:這個參數設定DiskStore(磁碟緩衝)的緩衝區大小。預設是30MB。每個Cache都應該有自己的一個緩衝區。23 diskExpiryThreadIntervalSeconds:磁碟失效線程已耗用時間間隔,預設是120秒。24 memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理記憶體。預設策略是LRU(最近最少使用)。你可以設定為FIFO(先進先出)或是LFU(較少使用)。25 clearOnFlush:記憶體數量最大時是否清除。26 memoryStoreEvictionPolicy:可選策略有:LRU(最近最少使用,預設策略)、FIFO(先進先出)、LFU(最少訪問次數)。27 FIFO,first in first out,這個是大家最熟的,先進先出。28 LFU, Less Frequently Used,就是上面例子中使用的策略,直白一點就是講一直以來最少被使用的。如上面所講,緩衝的元素有一個hit屬性,hit值最小的將會被清出緩衝。29 LRU,Least Recently Used,最近最少使用的,緩衝的元素有一個時間戳記,當緩衝容量滿了,而又需要騰出地方來緩衝新的元素的時候,那麼現有緩衝元素中時間戳記離目前時間最遠的元素將被清出緩衝。30 -->31 <defaultCache32 eternal="false"33 maxElementsInMemory="1000"34 overflowToDisk="false"35 diskPersistent="false"36 timeToIdleSeconds="0"37 timeToLiveSeconds="600"38 memoryStoreEvictionPolicy="LRU"39 />40 <cache41 name="demo"42 eternal="false"43 maxElementsInMemory="100"44 overflowToDisk="false"45 diskPersistent="false"46 timeToIdleSeconds="0"47 timeToLiveSeconds="300"48 memoryStoreEvictionPolicy="LRU"49 />50 51 </ehcache>
View Code
其實緩衝無非就是減少資料庫的查詢操作,接下來簡單說下在 代碼中的使用方法,
先把ehcache 配置到 spring中(下面是使用spring-boot)
package com.config;import org.springframework.cache.annotation.EnableCaching;import org.springframework.cache.ehcache.EhCacheCacheManager;import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.ClassPathResource;import javax.persistence.Cache;/** * Created by ding on 2017-04-25. */@Configuration@EnableCaching//標記啟動緩衝public class CacheConfiguration { //ehcache 主要的管理器 EhcacheManager @Bean public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){ EhCacheManagerFactoryBean factoryBean=new EhCacheManagerFactoryBean(); factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml")); factoryBean.setShared(true);//也說是說通過這個來設定cache的基地是這裡的Spring獨用,還是跟別的(如hibernate的Ehcache共用) return factoryBean; } @Bean public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean factoryBean){ System.out.println("CacheConfiguration.ehcacheManager()"); return new EhCacheCacheManager(factoryBean.getObject()); }}
留意下java代碼@cacheAble 註解中的 value 對應 ehcache.xml中定義的cache的name屬性(沒有找到,就會用預設的配置)
@cacheAble 註解中的 key 就是緩衝的key,調用方法的時候,就是根據這個key是否存在緩衝中 從而決定是否進入方法
注意:@cacheable註解只能用在實作類別中,不能再介面中使用!!!
@Servicepublic class InfoServiceImpl { //value屬性工作表示使用哪個緩衝策略,緩衝策略在ehcache.xml中 //LRU(最近最少使用,預設策略)、FIFO(先進先出)、LFU(最少訪問次數) public static final String DEMO_CACHE_NAME = "demo";//這個demo和ehcache中的 name對應 //所有key 中的單引號 都不能少,不然會被識別成一個對象 @Cacheable(value = DEMO_CACHE_NAME, key = "'info_'+#id") public String findNameById(Integer id) {
//Cacheable 不存在這個key 才會進入方法 System.out.println("沒有走緩衝:" + id); return "zhangsan"; } @CacheEvict(value = DEMO_CACHE_NAME, key = "'info_'+#id")//清除緩衝:是根據這個key來清理的,沒有找到key對應的緩衝就沒清理了public void delete(Integer id) { //資料庫刪除操作……(略) } @CachePut(value = DEMO_CACHE_NAME, key = "'info_'+#infos.getId()") public String update(String str) { /*
@cachePut 表示重新放入緩衝對象,不管是否存在這個key的緩衝,所以一定會進入方法
*/ return str; } }