---
title: "ehcache2.0配置 "
author: "714501466"
date: "2012-08-24T17:36:01Z"
tags: []
categories: ["行业应用"]
slug: "ehcache2-0"
draft: false
lastmod: "2012-08-24T17:39:05Z"
id: 1662083
url: "https://714501466.iteye.com/blog/1662083"
---

Ehcache是一个用Java实现的使用简单，高速，实现线程安全的缓存管理类库，ehcache提供了用内存，磁盘文件存储，以及分布式存储方式等多种灵活的cache管理方案。同时ehcache作为开放源代码项目，采用限制比较宽松的Apache License V2.0作为授权方式，被广泛地用于Hibernate,  Spring，Cocoon等其他开源系统。

此处只做简单使用并未涉及监听及分布式存储

ehcache.xml文件配置

&lt;ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:noNamespaceSchemaLocation="ehcache.xsd"  
         updateCheck="false" monitoring="autodetect"  
         dynamicConfig="true"&gt;

    &lt;diskStore path="java.io.tmpdir"/&gt;

    &lt;defaultCache  
            maxElementsInMemory="10000"  
            eternal="false"  
            timeToIdleSeconds="120"  
            timeToLiveSeconds="120"  
            overflowToDisk="true"  
            diskSpoolBufferSizeMB="30"  
            maxElementsOnDisk="10000000"  
            diskPersistent="false"  
            diskExpiryThreadIntervalSeconds="120"  
            memoryStoreEvictionPolicy="LRU"  
     /&gt;

     &lt;cache name="sampleCache1"  
           maxElementsInMemory="10000"  
           maxElementsOnDisk="10000"  
           eternal="false"  
           overflowToDisk="true"  
           diskSpoolBufferSizeMB="20"  
           timeToIdleSeconds="300"  
           timeToLiveSeconds="600"  
           memoryStoreEvictionPolicy="LFU"  
           transactionalMode="off"  
       /&gt;

&lt;/ehcache&gt;

**defaultCache**是不能被删除的，如果没有defaultCache运行时会报错

**diskStore**:是指ehcache数据缓存到硬盘上时的缓存路径，系统会在此目录下创建          cache\_name.data以及cache\_name.index文件  
  其**path**属性值可以为：**user.home** -用户主目录 (C:\\Documents and Settings\\Administrator)  
                      **user.dir**  -用户当前目录 (F:\\JAVA\\microblog  项目位置)  
                      **java.io.tmpdir** 默认文件临时路径 (C:\\DOCUME~1\\ADMINI~1\\LOCALS~1\\Temp\\)  
                      也可以**自定义路径** 如： D:/temp/cache

**cache标签属性：**  
   **name**:cache名称  
   **maxElementsInMemory**：设定内存中创建对象的最大值  
   **maxElementsOnDisk**：设定硬盘上缓存的对象最大值  
   **eternal**：设置元素（译注：内存中对象）是否永久驻留。如果是，将忽略超时限制且元素永不消亡，默认为false，若设为true时timeout属性均无效  
   **overflowToDisk**：设置当内存中缓存达到maxInMemory 限制时元素是否可写到磁盘上，此时缓存到硬盘上的对象必须是实现了Serializable接口的才行  
   **diskSpoolBufferSizeMB**：将内存中数据转移到硬盘时的缓冲区大小  
   **timeToIdleSeconds**：设置某个元素消亡前的停顿时间。也就是在一个元素消亡之前，两次访问时间的最大时间间隔值。这只能在元素不是永久驻留时有效（译注：如果对象永恒不灭，则设置该属性也无用）  
    **timeToLiveSeconds**：为元素设置消亡前的生存时间。也就是一个元素从构建到消亡的最大时间间隔值。这只能在元素不是永久驻留时有效  
    **memoryStoreEvictionPolicy**：缓存的清空策略  
            1 FIFO，先进先出  
            2 LFU，最少被使用，缓存的元素有一个hit属性，hit值最小的将会被清出缓存。  
            3 LRU，最近最少使用的，缓存的元素有一个时间戳，当缓存容量满了，而又需要腾出地方来缓存新的元素的时候，那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。  
    **diskPersistent**：在VM重启的时候是否持久化磁盘缓存，默认是false。若为true磁盘数据会混村到内存中  
    **diskExpiryThreadIntervalSeconds**：磁盘缓存的清理线程运行间隔，默认是120秒

 

 

 

转[http://luyao123127.iteye.com/blog/1300832](http://luyao123127.iteye.com/blog/1300832)