在系列(一)中,提到Ehcache提供了三種清空策略.那麼如何設置相應的參數呢? java
Ehcache提供了配置文件的方式,也提供了參數傳遞的方式. 算法
配置文件src/config/cache.xml 緩存
<cache name="testCache" maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />
初始化時參數傳遞: this
public Cache(String name, int maxElementsInMemory, MemoryStoreEvictionPolicy memoryStoreEvictionPolicy, boolean overflowToDisk, String diskStorePath, boolean eternal, long timeToLiveSeconds, long timeToIdleSeconds, boolean diskPersistent, long diskExpiryThreadIntervalSeconds, RegisteredEventListeners registeredEventListeners) { this(new CacheConfiguration(name, maxElementsInMemory) .memoryStoreEvictionPolicy(memoryStoreEvictionPolicy) .overflowToDisk(overflowToDisk) .eternal(eternal) .timeToLiveSeconds(timeToLiveSeconds) .timeToIdleSeconds(timeToIdleSeconds) .diskPersistent(diskPersistent) .diskExpiryThreadIntervalSeconds(diskExpiryThreadIntervalSeconds), registeredEventListeners, null); }
各配置參數的含義: spa
maxElementsInMemory:緩存中容許建立的最大對象數
eternal:緩存中對象是否爲永久的,若是是,超時設置將被忽略,對象從不過時。 code
overflowToDisk:內存不足時,是否啓用磁盤緩存。
timeToIdleSeconds:緩存數據的鈍化時間,也就是在一個元素消亡以前,兩次訪問時間的最大時間間隔值,這隻能在元素不是永久駐留時有效,若是該值是 0 就意味着元素能夠停頓無窮長的時間。
timeToLiveSeconds:緩存數據的生存時間,也就是一個元素從構建到消亡的最大時間間隔值,這隻能在元素不是永久駐留時有效,若是該值是0就意味着元素能夠停頓無窮長的時間。
memoryStoreEvictionPolicy:緩存滿了以後的淘汰算法。 xml
public CacheConfiguration(String name, int maxEntriesLocalHeap) { this.name = name; verifyGreaterThanOrEqualToZero((long) maxEntriesLocalHeap, "maxEntriesLocalHeap"); this.maxEntriesLocalHeap = maxEntriesLocalHeap; }
最後附上Cache的UML類圖.不過這只是整個Ehcache實現的冰山一角.讓咱們在後面的Blog中慢慢揭開她那神祕的面紗! 對象