1. 程式人生 > >ehcache.xml中配置及相關

ehcache.xml中配置及相關

<diskStore>是用來配置ehcache的磁碟儲存的,磁碟儲存可以儲存記憶體中驅除過來的元素,也可以在系統重啟的時候將記憶體中的快取資訊儲存起來,供系統重新啟動後使用。

一、ehcache.xml中關於<diskStore>的配置與註解

首先看下ehcache.xml配置檔案中的資訊:

    <!--

    DiskStore configuration

    =======================

    The diskStore element is optional. To turn off disk store path creation, comment out the diskStore

    element below.

    Configure it if you have disk persistence enabled for any cache or if you use

    unclustered indexed search.

    If it is not configured, and a cache is created which requires a disk store, a warning will be

     issued and java.io.tmpdir will automatically be used.

    diskStore has only one attribute - "path". It is the path to the directory where

    any required disk files will be created.

    If the path is one of the following Java System Property it is replaced by its value in the

    running VM. For backward compatibility these should be specified without being enclosed in the ${token}

    replacement syntax.

    The following properties are translated:

    * user.home - User's home directory

    * user.dir - User's current working directory

    * java.io.tmpdir - Default temp file path

    * ehcache.disk.store.dir - A system property you would normally specify on the command line

      e.g. java -Dehcache.disk.store.dir=/u01/myapp/diskdir ...

    Subdirectories can be specified below the property e.g. java.io.tmpdir/one

    -->

    <diskStore path="java.io.tmpdir"/>  

以上資訊是關於<diskStore>配置的註解,註解主要包含幾個重要的內容:

1、diskStore元素是可選的,非必須的。如果不使用磁碟儲存,只需要將diskStore註釋掉即可;如果使用,需要在ehcache.xml檔案中的ehcahce元素下的定義一個diskStore元素並指定其path屬性。

2、由diskStore元素是定義在ehcache元素下我們看出diskStore在CacheManager範圍內是共享的,其是執行緒安全的

3、對於任何快取,如果你已經激活了overflowToDisk或diskPersistent,就要配置磁碟儲存器。

(關於overflowToDisk或diskPersistent的配置,詳見配置檔案ehcache.xml詳解(1) )

4、DiskStore中驅除元素跟MemoryStore中驅除元素的規則是不一樣的。當往DiskStore中新增元素且此時DiskStore中的容量已經超出限制時將採用LFU(最不常用)驅除規則將對應的元素進行刪除,而且該驅除規則是不可配置的(通過cache中的diskExpiryThreadIntervalSeconds屬性完成)
5、path屬性如果是下述Java系統屬性之一,他將會被執行中的VM中的值替換。為了向後相容,這些應該被特別規定,而不會被${token}替換語法封閉。

6、path屬性可以配置的目錄有:

  •     user.home(使用者的家目錄)
  •     user.dir(使用者當前的工作目錄)
  •     java.io.tmpdir(預設的臨時目錄)
  •     ehcache.disk.store.dir(ehcache的配置目錄)
  •     絕對路徑(如:d:\\ehcache)

子目錄的話可以這樣指定:user.home/ehcache。

此外需要注意的是因為DiskStore是把資訊存放在磁碟上的,所以我們存放在磁碟上的元素必須是可以序列化的。CacheManager的DiskStore路徑一旦設定好了之後將不能再更改。如果硬是更改了,那麼我們的CacheManager需要基於新的路徑重新建立。

二、相關知識補充

1、 JAVA系統屬性之user.home

我們可以通過程式碼System.getProperty("user.home")讀取JAVA系統的user.home屬性的值,也可以用同樣的方法讀取到:user.dir和java.io.tmpdir

例如我們在web專案中,編寫如下程式碼:

public static void main(String[] args) {		
	String userHome = System.getProperty("user.home");
	String userDir = System.getProperty("user.dir");
	String tmpDir = System.getProperty("java.io.tmpdir");
		
	System.out.println("userHome:"+userHome);
	System.out.println("userDir:"+userDir);
	System.out.println("tmpDir:"+tmpDir);
}

執行tomcat可以在控制檯中看到相應的列印資訊:
userHome : C:\Documents and Settings\yf
userDir : D:\tomcat\apache-tomcat-6.0.37\bin
tmpDir : D:\tomcat\apache-tomcat-6.0.37\temp

=============================================
System.getProperty("user.home")方法先去讀取登錄檔中HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders下的Desktop鍵值做為user.dir,再取它的上一級目錄做為user.home

開啟登錄檔編輯器,定位到上面的鍵值,你可以發現Desktop的值是%USERPROFILE%\桌面這種形式。

%USERPROFILE%對應C:\Documents and Settings\%使用者名稱%。對於Administrator使用者,這裡取得的Desktop自然是C:\Documents and Settings\Administrator\桌面.那麼user.home就應該是C:\Documents and Settings\Administrator

user.dir是使用者當前的工作目錄,在tomcat中對應的目錄是bin目錄,所以user.dir指向tomcat/bin
java.io.tmpdir是預設的臨時目錄,tomcat的臨時目錄是temp目錄,所以這裡java.io.tmpdir指向tomcat/temp