外出旅行、冬季保暖得常備戶外襪、速幹襪、加厚襪子哦。 猛戳樂途驛站http://zhoupa1188.taobao.com搶購品牌男女式加厚戶外襪子,coolmax、全棉、保暖、吸汗、速幹、登山、徒步襪子。滿10包郵 |
ehcache 的配置可以通過在 XML文件中聲明,或者通過構造函數中指定參數進行程序控制。
兩種方法都能很好的支持。不過將緩存的配置和實際代碼分離開來更好一些,這有以下幾個好處:
- 便於在一個地方保存所有的配置信息
緩存使用內存或磁盤空間需要仔細調整和規劃,你可以在一個配置文件中獲取所有信息。當然你也可以都在代碼中搞定,但檢查起來就沒那麼方便。
- 便於在部署時修改緩存的配置
- 配置錯誤便於在啓動時被發現,以免在運行時才暴露出錯誤
- 如果存在一個缺省配置,那麼它總是會被裝載並生效
雖然缺省配置不是必需的,但如果沒有加載缺省配置,以編程方式創建命名cance(created by name) 就會產生錯誤。
動態修改緩存配置
緩存服務啓動之後,其配置一般不能再改變。但從 ehcache2.0之後,特定的緩存配置參數可以在運行時動態修改。在當前版本的ehcache,中包含以下幾個參數。
- timeToLive
一個被緩存的對象(Element)保存在緩存中的最大秒數,無論其是否被使用。超過這個時間限制,該對象將無法從緩存中獲取。默認值是0,表示沒有限制。
- timeToIdle
一個被緩存的對象(Element)保存在緩存中未被訪問的最大秒數。超過這個時間限制,該對象將無法從緩存中獲取。默認值是0,表示沒有限制。 - 本地緩存參數 maxEntriesLocalHeap, maxBytesLocalHeap, maxBytesLocalOffHeap, maxEntriesLocalDisk, maxBytesLocalDisk.
- memory-store eviction policy (內存驅逐策略)
- CacheEventListeners 可以動態增或刪除
注意Cache 的 "eternal" (永恆)屬性,如果設置爲true,重置 "timeToLive" 和 timeToIdle" 就都沒用了,任何對象都不會過期。
下面的例子演示如何動態修改使用中的緩存。
Cache cache = manager.getCache("sampleCache");
CacheConfiguration config = cache.getCacheConfiguration();
config.setTimeToIdleSeconds(60);
config.setTimeToLiveSeconds(120);
config.setmaxEntriesLocalHeap(10000);
config.setmaxEntriesLocalDisk(1000000);
緩存參數也可以被凍結,防止修改:
Cache cache = manager.getCache("sampleCache");
cache.disableDynamicFeatures();
在 ehcache.xml中,將<ehcache>的dynamicConfig 屬性設置爲"false", 可以禁止動態修改緩存參數
動態修改分佈式緩存配置
和獨立緩存一樣,操縱分佈式緩存的配置也需要通過cache.getCacheConfiguration()方法獲得 CacheConfiguration 對象,通過它的一系列 set 方法進行修改。
下表提供瞭如何動態修改一個Terracotta 集羣的公共配置選項的一些信息。表頭的"Scope"字段表示配置的變更能夠在哪裏生效,可以是以下幾個值。
- Client – The Terracotta client where the CacheManager runs.
- TSA – The Terracotta Server Array for the cluster.
- BOTH – Both the client and the TSA.
注意 "Both" 是分佈式含義,也就是說對所有客戶端都會生效
Configuration Option | Dynamic | Scope | Notes |
Cache name | NO | TSA | |
Nonstop | NO | Client | Enable High Availability。支持高可靠性 |
Timeout | YES | Client | For nonstop. |
Timeout Behavior | YES | Client | For nonstop. |
Immediate Timeout When Disconnected | YES | Client | For nonstop. |
Time to Idle | YES | BOTH | |
Maximum Entries or Bytes in Local Stores | YES | Client | This and certain other sizing attributes that are part of ARC may be pooled by the CacheManager, creating limitations on how they can be changed. |
Time to Live | YES | BOTH | |
Maximum Elements on Disk | YES | TSA | |
Persistence Strategy | N/A | N/A | |
Disk Expiry Thread Interval | N/A | N/A | |
Disk Spool Buffer Size | N/A | N/A | |
Overflow to Off-Heap | N/A | N/A | |
Maximum Off-heap | N/A | N/A | Maximum off-heap memory allotted to the TSA. |
Eternal | YES | BOTH | |
Clear on Flush | NO | Client | |
Copy on Read | NO | Client | |
Copy on Write | NO | Client | |
Memory Store Eviction Policy | NO | Client | |
Statistics | YES | Client | Cache statistics. Change dynamically withcache.setStatistics(boolean) method. |
Logging | NO | Client | Ehcache and Terracotta logging is specified in configuration. However,cluster events can be set dynamically. |
Consistency | NO | Client | It is possible to switch to and from bulk mode. |
Synchronous Writes | NO | Client |
如果一個L1 Cache 被設置成了非動態的,要讓他的配置變更生效就比較麻煩,要做以下操作。刪除已存在的cache,用新的配置新建一個同名cache,當然要在同一個 CacheManager 中。 重新啓動CacheManager. 親,新配置裏的Cache名字一定要和就配置一樣。