`
anson_xu
  • 浏览: 505801 次
  • 性别: Icon_minigender_1
  • 来自: 惠州
社区版块
存档分类

二级缓存的配置 配置ehcache.xml文件

    博客分类:
  • SSH
阅读更多

原文出处:http://i.cn.yahoo.com/en.wan1982/blog/p_9/

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.xml

    《深入解析Hibernate二级缓存配置文件ehcache.xml》 在Java企业级开发中,数据库的高效访问是系统性能优化的关键之一。Hibernate作为一种流行的ORM(对象关系映射)框架,提供了强大的数据持久化能力。然而,频繁的...

    mybatis ehcache 1.0 ehcache.xsd 提示文件

    集成MyBatis和Ehcache时,我们需要在MyBatis的全局配置文件(`mybatis-config.xml`)中指定`ehcache.xml`的位置,并启用二级缓存: ```xml ... &lt;cache type="org.mybatis.cache.ehcache.EhcacheCache"&gt; ...

    Hibernate EhCache 二级缓存配置.docx

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

    为Spring集成的Hibernate配置二级缓存

    3. **配置Spring**:在Spring的配置文件(如`applicationContext.xml`)中,配置Hibernate SessionFactory,并注入二级缓存配置。以下是一个配置示例: ```xml &lt;bean id="sessionFactory" class="org.spring...

    ehcache二级缓存配置文件

    **Ehcache二级缓存配置详解** Ehcache是一个广泛使用的开源Java缓存库,它提供了内存和磁盘存储的缓存解决方案,适用于提高应用程序性能和减少数据库负载。在Java应用中,尤其是在Spring框架中,Ehcache常被用作二...

    Hibernate二级缓存架包.rar

    本文将详细讲解Hibernate二级缓存的概念、工作原理,并结合给定的"ehcache.xml"配置文件和"Hibernate二级缓存架包",阐述如何设置和使用二级缓存。 一、Hibernate二级缓存概述 1.1 一级缓存与二级缓存的区别 一级...

    Spring集成的Hibernate配置二级缓存

    以EhCache为例,我们需要在项目中引入ehcache-core或ehcache的依赖,并在Hibernate配置文件(hibernate.cfg.xml或persistence.xml)中启用二级缓存,添加如下配置: ```xml &lt;property name="hibernate.cache.use_...

    Hibernate4二级缓存Ehcache案例

    在这个“Hibernate4二级缓存Ehcache案例”中,我们将深入探讨如何利用Ehcache作为Hibernate的二级缓存提供商,以提升应用性能。 首先,我们需要了解什么是二级缓存。一级缓存是Hibernate Session级别的缓存,每个...

    Hibernate中二级缓存ehcache缓存案例

    **二级缓存ehcache** ehcache是Hibernate官方推荐的二级缓存实现,它是一个高性能、轻量级的缓存框架。使用ehcache可以将频繁访问的数据存储在内存中,避免了频繁的数据库交互,从而提高了系统的响应速度。ehcache...

    hibernate一级和二级缓存配置与详解

    然后,在`hibernate.cfg.xml`配置文件中,指定缓存提供者,并开启二级缓存: ```xml &lt;property name="hibernate.cache.region.factory_class"&gt;org.hibernate.cache.ehcache.EhCacheRegionFactory ...

    hibernate二级缓存相关插件(EHcache)

    2. **配置Hibernate**:在`hibernate.cfg.xml`配置文件中启用二级缓存并指定EHcache为缓存提供者。例如: ```xml &lt;property name="hibernate.cache.use_second_level_cache"&gt;true &lt;property name="hibernate....

    Ehcache二级缓存.zip

    在这个"Ehcache二级缓存.zip"压缩包中,可能包含了实现Ehcache二级缓存的相关文件,如jar包、配置文件和文档等。 1. **Ehcache二级缓存**:在Java应用中,一级缓存通常指的是JVM内的内存缓存,而二级缓存则可以是...

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

    2. **配置Hibernate**:在Hibernate的配置文件`hibernate.cfg.xml`中,我们需要指定Ehcache作为二级缓存提供者。添加以下配置: ```xml &lt;property name="hibernate.cache.use_second_level_cache"&gt;true ...

    java缓存详解[借鉴].pdf

    一级缓存是指 Hibernate 自己的缓存机制,而二级缓存是指使用第三方缓存工具,如 EhCache。 EhCache 是一种流行的 Java 缓存工具,支持多种缓存策略和存储方式。EhCache 可以将缓存存储在内存中,也可以将其存储在...

    Hibernate_EHcache.doc

    2. 配置Hibernate:在hibernate.cfg.xml配置文件中,我们需要声明使用Ehcache作为二级缓存提供者。通过设置`hibernate.cache.provider_class`属性为`org.hibernate.cache.EhCacheProvider`,可以启用Ehcache插件。...

    EHCache插件.rar

    1. **配置EHCache**:在`hibernate.cfg.xml`中,需要添加EHCache插件的相关配置,包括引入EHCache的配置文件、开启二级缓存支持等。 2. **实体类注解**:对于需要缓存的实体类,可以使用`@Cacheable`注解标记,指定...

    springmvc4+spring4+hibernate5.1.3+二级缓存ehcache+fastjson配置

    Ehcache是Hibernate的一个可选二级缓存插件,用于存储数据库查询结果,减少对数据库的直接访问。当相同的数据再次被请求时,可以从缓存中快速获取,提高系统响应速度。在不使用缓存的情况下,可以通过配置关闭。 5...

    hibernate缓存ehcache用法

    然后,在Hibernate的配置文件(如hibernate.cfg.xml)中启用二级缓存并指定Ehcache配置文件: ```xml &lt;property name="hibernate.cache.use_second_level_cache"&gt;true &lt;property name="hibernate.cache.region....

    day37 05-HIbernate二级缓存:一级缓存更新同步到二级缓存及二级缓存配置文件

    本篇文章将深入探讨Hibernate的二级缓存机制,以及如何进行一级缓存与二级缓存的同步,同时还会介绍二级缓存的配置文件设置。 一级缓存是Hibernate默认提供的缓存,每个SessionFactory实例都有一个一级缓存。当对象...

    Hibernate二级缓存(Ehcache)

    首先,我们需要在Hibernate配置文件(hibernate.cfg.xml)中启用二级缓存,并指定Ehcache为缓存提供者: ```xml &lt;property name="hibernate.cache.use_second_level_cache"&gt;true &lt;property name="hibernate.cache....

Global site tag (gtag.js) - Google Analytics