`
coolworm
  • 浏览: 44217 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

配置ehcache.xml文件

阅读更多

1. 在Hibernate配置文件中设置:
    

<!-- Hibernate SessionFactory -->
    
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        
<property name="dataSource" ref="dataSource"/>
        
<property name="mappingResources">
        
<list>
            
<value>com/ouou/model/Videos.hbm.xml</value>   
         
</list>
         
</property>
        
<property name="hibernateProperties">
            
<props>
                
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                
<prop key="hibernate.current_session_context_class">thread</prop>
                
<prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
                
<prop key="hibernate.query.substitutions">true 'Y'false 'N'</prop>
                
<!--add ehcache-->
                
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                
<prop key="hibernate.cache.use_query_cache">false</prop><!-- 是否使用查询缓存 -->
                
<!--
                
<prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
                
<prop key="hibernate.show_sql">true</prop>
                
-->
                
<!--<prop key="hibernate.transaction.auto_close_session">true</prop>-->
                
<prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
                
<!-- Create/update the database tables automatically when the JVM starts up
                 
<prop key="hibernate.hbm2ddl.auto">update</prop> -->
                
<!-- Turn batching off for better error messages under PostgreSQL -->
                
<prop key="hibernate.jdbc.batch_size">25</prop>
                
<!--
                
<prop key="hibernate.connection.pool_size">10</prop>
                
-->
            
</props>
        
</property>
    
</bean>


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

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

 <ehcache>

    
<!-- Sets the path to the directory where cache .data files are created.

     If the path is a Java System Property it is replaced by
     its value in the running VM.
     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 -->
     
<!--<diskStore path="java.io.tmpdir"/>-->
     
<diskStore path="/data/ehcache"/>

    
<!--Default Cache configuration. These will applied to caches programmatically created through
        the CacheManager.

        The following attributes are required:

        maxElementsInMemory            
- Sets the maximum number of objects that will be created in memory
        eternal                                     
- Sets whether elements are eternal. If eternal,  timeouts are 
                                                            ignored and the element is never expired.
        overflowToDisk                      
- Sets whether elements can overflow to disk when the in-memory cache
                                                        has reached the maxInMemory limit.

        The following attributes are optional:
        timeToIdleSeconds           
- Sets the time to idle for an element before it expires.
                                                        i.e. The maximum amount of time between accesses before an
                                                        element expires Is only used 
if the element is not eternal.
                                                        Optional attribute. A value of 
0 means that an Element can idle
                                                       
for infinity.The default value is 0.
        timeToLiveSeconds             
- Sets the time to live for an element before it expires.
                                                         i.e. The maximum time between creation time and when an element 
                                                         expires.  Is only used 
if the element is not eternal.
                                                         Optional attribute. A value of 
0 means that and Element can live
                                                        
for infinity.
                                                        The 
default value is 0.
        diskPersistent                           
- Whether the disk store persists between restarts of the Virtual
                                                             Machine.
                                                         The 
default value is false.
        diskExpiryThreadIntervalSeconds   
- The number of seconds between runs of the disk expiry thread. 
                                                         The 
default value  is 120 seconds.
        
-->

    
<defaultCache
        maxElementsInMemory
="10000"
        eternal
="false"
        overflowToDisk
="true"
        timeToIdleSeconds
="120"
        timeToLiveSeconds
="120"
        diskPersistent
="false"
        diskExpiryThreadIntervalSeconds
="120"/>
    
<cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000" 
     eternal
="true" overflowToDisk="true"/>
    
<cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="5" eternal="false"
    timeToLiveSeconds
="120" overflowToDisk="true"/>
    
<cache name="userCache" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds=
        "
600"    timeToLiveSeconds="600" overflowToDisk="false" diskPersistent="false"/>
    
<cache name="com.ouou.webapp.util.OuouMethodIntecepter" maxElementsInMemory="100000" 
    eternal
="false" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="false"
    diskPersistent
="false"/>
    
<cache name="bbcode" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds="600"
    timeToLiveSeconds
="600" 
    overflowToDisk
="false" diskPersistent="false"/>
    
<cache name="com.ouou.model.Videos" maxElementsInMemory="10000"  eternal="false" 
    overflowToDisk
="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>
    
<cache name="com.ouou.model.Tags" maxElementsInMemory="10000"  eternal="false"
    overflowToDisk
="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>
</ehcache>


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

3、 选择缓存策略依据:

<cache  usage="transactional|read-write|nonstrict-read-write|read-only" (1)/>
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

分享到:
评论

相关推荐

    ehcache.xsd_ehcache.xml代码提示.rar

    【标题解析】:“ehcache.xsd_ehcache.xml代码提示.rar”这个标题表明这是一个与Ehcache缓存系统相关的资源包,主要目的是为Ehcache的配置文件ehcache.xml提供代码提示功能。Ehcache是一个广泛使用的开源Java缓存...

    mybatis ehcache 1.0 ehcache.xsd 提示文件

    `ehcache.xsd`包含了如缓存区域(cache)、缓存策略(eviction),以及缓存更新策略(updateStrategy)等元素的定义,确保了开发者在配置Ehcache时遵循正确的格式。 下面我们将深入讨论`ehcache.xml`配置文件。在...

    ehcache.xml

    本文将重点解析 Ehcache 的配置文件 `ehcache.xml` 及其核心方法,帮助开发者更好地理解和应用 Ehcache。 首先,我们来详细剖析 `ehcache.xml` 配置文件的各个部分: 1. **磁盘存储配置**:通过 `&lt;diskStore&gt;` ...

    shiro-ehcache.xml

    在maven项目中,此shiro-ehcache.xml配置文件放在resources下,在applicationContext.xml中,用于shiro缓存管理器所配置,然后给shiro安全管理器配置此缓存管理器

    ehcache的配置参数详解

    本文将深入探讨ehcache.xml配置文件中的关键参数及其作用,帮助开发者更有效地管理和优化缓存策略。 ### 1. `defaultCache` 标签 `defaultCache` 是ehcache.xml中一个重要的标签,用于定义所有未显式指定缓存策略...

    ehcache.zip

    4. **ehcache.xsd**:这个文件是Ehcache XML配置文件的架构定义,用于验证配置文件的语法正确性。它定义了所有可用的配置元素和属性,以及它们的结构和限制。当你编写或修改`ehcache.xml`时,这个XSD文件可以作为...

    ehcache.xsd

    在springboot上配置ehcache时用到的xld文件,在编写xml时会报找不到ehcache.xsd文件,把这个文件放到resources目录底下即可

    java web项目里ehcache.xml介绍

    Ehcache 的配置文件通常是 `ehcache.xml`,它定义了缓存的行为和参数。 **ehcache.xml 配置详解** 1. **根元素 `&lt;ehcache&gt;`** - `xmlns:xsi`: 定义 XML Schema 实例的命名空间。 - `xsi:...

    mybatis整合ehcache的jar包+配置文件.zip

    2. **配置Ehcache**:Ehcache的配置主要通过`ehcache.xml`文件完成。这个文件中定义了缓存的策略、大小、过期时间等。例如,你可以设置一个名为`mybatisCache`的缓存,指定它的最大元素数量、内存和磁盘存储策略: ...

    springboot 整合ehcache+redis 通过配置文件切换缓存类型

    ehcache :添加依赖 pom.xml 2、添加配置文件ehcache.xml 3、添加注解@EnableCaching @Cacheable 4、插入缓存 5 读取缓存 6 设置缓存过期时间ehcache.xml --&gt;timeToLiveSeconds。 redis : 1、添加依赖 pom.xml 2、...

    ehcache.jar及源码

    学习Ehcache时,开发者需要理解其配置文件(通常为`ehcache.xml`),其中包含了缓存管理器的配置、缓存的设置等信息。此外,Ehcache与Spring框架的集成也是常见的应用场景,通过Spring的缓存抽象,可以方便地将...

    Ehcache(2): Ehcache实例在Eclipse中的配置 改进

    3. **配置Ehcache**:创建Ehcache的配置文件`ehcache.xml`,并放置在项目的类路径下。这个文件定义了缓存的策略,如缓存大小、存活时间等。一个简单的配置示例如下: ```xml &lt;ehcache xmlns:xsi=...

    ehcache-2.10.3-distribution.tar.gz

    描述提到"ehcache文档,里面包含了ehcache的xml文件以及ehcache.xsd文件,以及帮助文档",这表明这个压缩包内含以下几个关键组成部分: 1. **ehcache.xml**: 这是Ehcache的配置文件,用于定义缓存的行为和设置。...

    Hibernate EhCache 二级缓存配置.docx

    Hibernate EhCache 二级缓存配置 Hibernate EhCache 二级缓存配置是 Hibernate 框架中的一种缓存机制,它可以提高应用程序的性能和效率。...通过配置 EhCache 缓存插件和 Dept.hbm.xml 文件,可以实现二级缓存的功能。

    Mybatis整合第三方缓存ehcache.docx

    2. **配置 Ehcache**:创建一个 `ehcache.xml` 配置文件,定义缓存策略,如缓存大小、过期时间、存储路径等。例如: ```xml &lt;ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:...

    SpringBoo2.x,整合Ehcache3.x

    其次,Ehcache 3.x的配置与2.x版本有所不同,需要在`resources`目录下创建`ehcache.xml`文件,并按照Ehcache 3.x的格式编写配置。例如: ```xml &lt;?xml version="1.0" encoding="UTF-8"?&gt; ***' xmlns:eh='***' xsi...

    Hibernate_EHcache.doc

    3. 拷贝Ehcache配置文件:将Ehcache的配置文件(ehcache.xml)放入类路径,通常是项目源码目录(src/main/resources)下。这个配置文件定义了缓存的策略、大小、过期时间等参数,可以根据实际需求进行定制。 三、...

    ehcache-failsafe.xml

    ehcache的缓存配置文件

Global site tag (gtag.js) - Google Analytics