`
nannan408
  • 浏览: 1754816 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

hibernate 一级和二级缓存使用总结

 
阅读更多
   hibernate的缓存分为一级缓存和二级缓存,一级二级和我们常说的cpu的一级二级是不一样的。这里的一级说的是session的缓存,是hibernate内置的,不能卸载。二级说的是SessionFactory中的外置缓存,SessionFactory的内置缓存是放映射数据和sql语句的,程序不能更改,也不算二级缓存。二级缓存可以配置和更改,并且动态加载和卸载。Hibernate还为查询结果提供了一个查询缓存,它依赖于第二级缓存。
   二. 一级缓存的管理:当应用程序调用Session的save()、update()、savaeOrUpdate()、get()或load(),以及调用查询接口的 list()、iterate()或filter()方法时,如果在Session缓存中还不存在相应的对象,Hibernate就会把该对象加入到第一级缓存中。当清理缓存时,Hibernate会根据缓存中对象的状态变化来同步更新数据库。 Session为应用程序提供了两个管理缓存的方法: evict(Object obj):从缓存中清除参数指定的持久化对象。 clear():清空缓存中所有持久化对象。
   三. Hibernate二级缓存的管理:
  1. Hibernate二级缓存策略的一般过程如下:
     1) 条件查询的时候,总是发出一条select * from table_name where …. (选择所有字段)这样的SQL语句查询数据库,一次获得所有的数据对象。
     2) 把获得的所有数据对象根据ID放入到第二级缓存中。
     3) 当Hibernate根据ID访问数据对象的时候,首先从Session一级缓存中查;查不到,如果配置了二级缓存,那么从二级缓存中查;查不到,再查询数据库,把结果按照ID放入到缓存。
     4) 删除、更新、增加数据的时候,同时更新缓存。Hibernate二级缓存策略,是针对于ID查询的缓存策略,对于条件查询则毫无作用。为此,Hibernate提供了针对条件查询的Query Cache。
  2. 什么样的数据适合存放到第二级缓存中?
      1) 很少被修改的数据
      2) 不是很重要的数据,允许出现偶尔并发的数据
      3) 不会被并发访问的数据
      4) 参考数据,指的是供应用参考的常量数据,它的实例数目有限,它的实例会被许多其他类的实例引用,实例极少或者从来不会被修改。
  3. 不适合存放到第二级缓存的数据?
      1) 经常被修改的数据
      2) 财务数据,绝对不允许出现并发
      3) 与其他应用共享的数据。
  4. 常用的缓存插件 Hibernater二级缓存是一个插件,下面是几种常用的缓存插件:    
      ◆EhCache:可作为进程范围的缓存,存放数据的物理介质可以是内存或硬盘,对Hibernate的查询缓存提供了支持。
      ◆OSCache:可作为进程范围的缓存,存放数据的物理介质可以是内存或硬盘,提供了丰富的缓存数据过期策略,对Hibernate的查询缓存提供了支持。
      ◆SwarmCache:可作为群集范围内的缓存,但不支持Hibernate的查询缓存。   
      ◆JBossCache:可作为群集范围内的缓存,支持事务型并发访问策略,对Hibernate的查询缓存提供了支持。
      ◆memcached
     ◆redis
四、如何配置二级缓存
   4.1如果采用ehcached,配置如下:
       4.1.1 将相应的二级缓存组件jar包导入到classpath类路径下
        4.1.2在hibernate.cfg.xml文件中配置如下的信息:
                  <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
                 <property name="hibernate.cache.use_query_cache">true</property>
                 <property name="cache.use_second_level_cache">true</property>
                <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
      4.1.3. 指定哪些对象选要被缓存:这里既可以在hibernate.cfg.xml文件中配置,也可以在对应的hbm文件中配置
          4.1.3.1  hibernate.cfg.xml文件中配置如下:           
                 指定需要缓存的对象类型:这里可以在映射文件中配置,也可以在hibernate核心配置文件中进行配置
                  <class-cache class="com.hib.entity.Inf" usage="read-only" />            
         4.1.3.2  hbm文件中配置如下:
                 <cache usage="read-only"/> 在需要缓存的元素下添加<cache>元 素,根据需求使用相应的缓存级别              
       4.1.4.  在src目录下编写一个ehcache.xml文件,配置一些基本的缓存信息:
              一般配置信息如下:
              <?xml version="1.0" encoding="utf-8"?>
            <ehcache>     
                <diskStore path="D:/ehcache"/><!--如果缓存中的对象存储超过指定的缓存数量的对象存储的磁盘地址-->
                <!--全部默认的配置
                maxElementsInMemory在內存中最多存放多少个对象
                eternal对象是不是永远不变的,一般都是false
                timeToLiveSeconds如果这个对象超过了这个时间,就会从缓存中清除
                  -->
                <defaultCache
                maxElementsInMemory="500"
                eternal="false"
                timeToIdleSeconds="120"
                timeToLiveSeconds="120"
                overflowToDisk="true"
                />

                <!-- 针对指定的对象使用的缓存配置
                 name表示的是缓存的类对象的全路径名 -->
                <cache name="com.hibernate.Student"
                maxElementsInMemory="500"
                eternal="false"
                timeToIdleSeconds="50"
                timeToLiveSeconds="50"
                overflowToDisk="true"
                />
            </ehcache>
    4.1.3.5如果有spring,那么spring中的配置
        4.1.3.5.1.在类路径上ehcache.xml:

<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"/>


     <!--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"/>
       
     <!-- See http://ehcache.sourceforge.net/documentation/#mozTocId258426 for how to configure caching for your objects -->
</ehcache>

                   4.1。3.5.2.applicationContext-hibernate.xml里Hibernate SessionFactory配置:

     <!-- Hibernate SessionFactory -->
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
         <property name="dataSource" ref="dataSource"/>
         <property name="configLocation"><value>classpath:hibernate.cfg.xml</value></property>
         <!-- The property below is commented out b/c it doesn't work when run via
              Ant in Eclipse.   It works fine for individual JUnit tests and in IDEA ??
         <property name="mappingJarLocations">
             <list><value>file:dist/appfuse-dao.jar</value></list>
         </property>
         -->
         <property name="hibernateProperties">
             <props>
                 <prop key="hibernate.dialect">@HIBERNATE-DIALECT@</prop>
                 <!--<prop key="hibernate.show_sql">true</prop>-->
                 <prop key="hibernate.max_fetch_depth">3</prop>
                 <prop key="hibernate.hibernate.use_outer_join">true</prop>
                 <prop key="hibernate.jdbc.batch_size">10</prop>
                 <prop key="hibernate.cache.use_query_cache">true</prop>
                 <prop key="hibernate.cache.use_second_level_cache">true</prop>
                 <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                
<!--
                 <prop key="hibernate.use_sql_comments">false</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">0</prop> -->
             </props>
         </property>
         <property name="entityInterceptor">
            <ref local="auditLogInterceptor"/>
         </property>
     </bean>


启用class缓存:

    4.2其他缓存和spring也是差不多,基本上是导包,自身缓存配置,spring配置加上true.   
  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics