配置文件ehcache.xml详解(2)— <diskStore>配置及相关

来源:互联网 发布:电脑锣编程培训 编辑:程序博客网 时间:2024/06/05 00:36

<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、对于任何缓存,如果你已经激活了overflowToDiskdiskPersistent,就要配置磁盘存储器。
(关于overflowToDiskdiskPersistent的配置,详见配置文件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.dirjava.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





参考文章:
http://www.cnblogs.com/crazylqy/p/4238265.html  
http://blog.csdn.net/hudashi/article/details/7069750  


0 0