---
title: "配置ehcache.xml文件 "
author: "coolworm"
date: "2009-04-10T17:19:07Z"
tags: []
slug: "ehcache-xml"
draft: false
lastmod: "2009-04-10T17:19:07Z"
id: 365190
url: "https://coolworm.iteye.com/blog/365190"
---

1\. 在Hibernate配置文件中设置：  
    

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!-- Hibernate SessionFactory --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;property name="dataSource" ref="dataSource"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;property name="mappingResources"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;list&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            &lt;value&gt;com/ouou/model/Videos.hbm.xml&lt;/value&gt;     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)         &lt;/list&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)         &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;property name="hibernateProperties"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            &lt;props&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;prop key="hibernate.current\_session\_context\_class"&gt;thread&lt;/prop&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;prop key="hibernate.cglib.use\_reflection\_optimizer"&gt;false&lt;/prop&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;prop key="hibernate.query.substitutions"&gt;true 'Y', false 'N'&lt;/prop&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;!--add ehcache--&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;prop key="hibernate.cache.provider\_class"&gt;org.hibernate.cache.EhCacheProvider&lt;/prop&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;prop key="hibernate.cache.use\_query\_cache"&gt;false&lt;/prop&gt;&lt;!-- 是否使用查询缓存 --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;!--  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;prop key="hibernate.cache.provider\_configuration\_file\_resource\_path"&gt;/ehcache.xml&lt;/prop&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;prop key="hibernate.show\_sql"&gt;true&lt;/prop&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;!--&lt;prop key="hibernate.transaction.auto\_close\_session"&gt;true&lt;/prop&gt;--&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;prop key="connection.provider\_class"&gt;org.hibernate.connection.C3P0ConnectionProvider&lt;/prop&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;!-- Create/update the database tables automatically when the JVM starts up  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                 &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt; --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;!-- Turn batching off for better error messages under PostgreSQL --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;prop key="hibernate.jdbc.batch\_size"&gt;25&lt;/prop&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;!--  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                &lt;prop key="hibernate.connection.pool\_size"&gt;10&lt;/prop&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            &lt;/props&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/bean&gt;

    如果不设置“查询缓存”，那么hibernate只会缓存使用load()方法获得的单个持久化对象，如果想缓存使用findall()、 list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话，就需要设置hibernate.cache.use\_query\_cache true 才行

2.首先设置EhCache，建立配置文件ehcache.xml，默认的位置在class-path，可以放到你的src目录下：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;ehcache&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;!-- Sets the path to the directory where cache .data files are created.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     If the path is a Java System Property it is replaced by  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     its value in the running VM.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     The following properties are translated:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     user.home - User's home directory  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     user.dir - User's current working directory  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     java.io.tmpdir - Default temp file path --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;!--&lt;diskStore path="java.io.tmpdir"/&gt;--&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;diskStore path="/data/ehcache"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;!--Default Cache configuration. These will applied to caches programmatically created through  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        the CacheManager.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        The following attributes are required:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        maxElementsInMemory            - Sets the maximum number of objects that will be created in memory  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        eternal                                     - Sets whether elements are eternal. If eternal,  timeouts are   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                                                            ignored and the element is never expired.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        overflowToDisk                      - Sets whether elements can overflow to disk when the in-memory cache  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                                                        has reached the maxInMemory limit.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        The following attributes are optional:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        timeToIdleSeconds            - Sets the time to idle for an element before it expires.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                                                        i.e. The maximum amount of time between accesses before an  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                                                        element expires Is only used if the element is not eternal.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                                                        Optional attribute. A value of 0 means that an Element can idle  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                                                        for infinity.The default value is 0.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        timeToLiveSeconds              - Sets the time to live for an element before it expires.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                                                         i.e. The maximum time between creation time and when an element   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                                                         expires.  Is only used if the element is not eternal.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                                                         Optional attribute. A value of 0 means that and Element can live  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                                                         for infinity.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                                                        The default value is 0.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        diskPersistent                           - Whether the disk store persists between restarts of the Virtual  
                                                             Machine.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                                                         The default value is false.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        diskExpiryThreadIntervalSeconds   - The number of seconds between runs of the disk expiry thread.   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)                                                         The default value  is 120 seconds.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;defaultCache  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        maxElementsInMemory="10000"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        eternal="false"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        overflowToDisk="true"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        timeToIdleSeconds="120"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        timeToLiveSeconds="120"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        diskPersistent="false"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        diskExpiryThreadIntervalSeconds="120"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000"   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     eternal="true" overflowToDisk="true"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="5" eternal="false"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    timeToLiveSeconds="120" overflowToDisk="true"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;cache name="userCache" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds=  
        "600"    timeToLiveSeconds="600" overflowToDisk="false" diskPersistent="false"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;cache name="com.ouou.webapp.util.OuouMethodIntecepter" maxElementsInMemory="100000"   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="false"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    diskPersistent="false"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;cache name="bbcode" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds="600"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    timeToLiveSeconds="600"   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    overflowToDisk="false" diskPersistent="false"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;cache name="com.ouou.model.Videos" maxElementsInMemory="10000"  eternal="false"   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;cache name="com.ouou.model.Tags" maxElementsInMemory="10000"  eternal="false"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/ehcache&gt;

以com.ouou.model.Videos为例子  
在Videos.hbm.xml中配置：  
&lt;class name="Videos" table="TEST" lazy="false"&gt;  
  &lt;cache usage="read-write" region="ehcache.xml中的name的属性值"/&gt;注意：这一句需要紧跟在class标签下面，其他位置无效。  
hbm文件查找cache方法名的策略：如果不指定hbm文件中的region="ehcache.xml中的name的属性值"，则使用name名为com.ouou.model.Videos的cache，  
如果不存在与类名匹配的cache名称，则用defaultCache。  
如果Videos包含set集合，则需要另行指定其cache  
例如Videos包含Tags集合，则需要  
添加如下配置到ehcache.xml中  
&lt;cache name="com.ouou.model.Tags"  
        maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"  
        timeToLiveSeconds="120" overflowToDisk="false" /&gt;  
另，针对查询缓存的配置如下：  
&lt;cache name="org.hibernate.cache.UpdateTimestampsCache"  
        maxElementsInMemory="5000"  
        eternal="true"  
        overflowToDisk="true"/&gt;  
&lt;cache name="org.hibernate.cache.StandardQueryCache"  
        maxElementsInMemory="10000"  
        eternal="false"  
        timeToLiveSeconds="120"  
        overflowToDisk="true"/&gt;

3、 选择缓存策略依据：

&lt;cache  usage="transactional|read-write|nonstrict-read-write|read-only" (1)/&gt;  
ehcache不支持transactional，其他三种可以支持。  
read-only：无需修改， 那么就可以对其进行只读 缓存，注意，在此策略下，如果直接修改数据库，即使能够看到前台显示效果，  
但是将对象修改至cache中会报error，cache不会发生作用。另：删除记录会报错，因为不能在read-only模式的对象从cache中删除。  
read-write：需要更新数据，那么使用读/写缓存 比较合适，前提：数据库不可以为serializable transaction isolation level  
（序列化事务隔离级别）  
nonstrict-read-write：只偶尔需要更新数据（也就是说，两个事务同时更新同一记录的情况很不常见），也不需要十分严格的事务隔离，  
那么比较适合使用非严格读/写缓存策略。

4、 调试时候使用log4j的log4j.logger.org.hibernate.cache=debug，更方便看到ehcache的操作过程，主要用于调试过程,实际应用发布时候，请注释掉，以免影响性能。

5、 使用ehcache，打印sql语句是正常的，因为query cache设置为true将会创建两个缓存区域：一个用于保存查询结果集 (  
org.hibernate.cache.StandardQueryCache)；另一个则用于保存最近查询的一系列表的时间戳(org.hibernate.cache.UpdateTimestampsCache)。  
请注意：在查询缓存中，它并不缓存结果集中所包含的实体的确切状态；它只缓存这些实体的标识符属性的值、以及各值类型的结果。  
需要将打印sql语句与最近的cache内容相比较，将不同之处修改到cache中，所以查询缓存通常会和二级缓存一起使用。

英文参考资料:http://ehcache.sourceforge.net/documentation/#mozTocId258426  
博文参考:http://blog.csdn.net/yun15291li/archive/2006/02/21/604095.aspx  
                 http://zyl.iteye.com/blog/68369  
其他:http://dev.yesky.com/157/2557157.shtml