【转】Hibernate缓存机制

hibernate提供了缓存功能,对于不经常修改的数据可以提高查询效率,若对于经常修改的数据,使用缓存会对于查询效率会有影响,效率会下降。

hibernate提供了两个级别的缓存:

  一级缓存,是在session作用域中的缓存,当session关闭时,这个session中的缓存会自动清空。

  二级缓存,在sessionfactory级别的缓存,我们要使用二级缓存需要配置。

hibernate会帮我们管理缓存,什么时候将数据插入的缓存,什么时候从缓存中读取数据。一般情况如下:

  当我们调用session的save、update、saveOrUpdate、load、get、list、iterate、lock等方法时hibernate会将操作的数据添加进一级缓存,若使用二级缓存也会添加进二级缓存。当我们使用get、load方法时,若缓存中有要查找的数据,就会从缓存中读取,不再查询数据库,若缓存中没有,则执行数据库查询。

如下面代码使用一级缓存:


package test;

import java.util.Date;
import model.Person;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import util.HibernateUtil;

public class PersonTest {
    @Test
    public void oneLevelCache(){
        Session session = HibernateUtil.getSession() ;
        Transaction transaction = session.beginTransaction() ;
        Person person = new Person() ;
        person.setName("person") ;
        person.setBirthdate(new Date()) ;
        session.save(person) ;         //保存Person对象 id为1    同时添加进session加进缓存
        
        Person person2 = (Person) session.get(Person.class, 1L) ;   //没有执行select查询     这里从缓存中读取id为1的对象      
        System.out.println(person2.getName());
        
    
        transaction.commit() ;
        session.evict(person);      //evict方法清除person对象的缓存   即id为1的缓存    若evict在事务提交之前   则不会保存person对象  
        
        Person person3 = (Person) session.get(person.getClass(), 1L) ;   //因为session缓存中已经清除id为1的缓存   这里执行select查询     同时加入缓存
        System.out.println(person3.getName());
        
        //hql、criteria等查询不会从缓存中读取数据
        Person person4 = (Person) session.createQuery("from Person person where person.id = :id ").setLong("id", 1L).uniqueResult() ;
        
        session.clear() ;    //清除session中所有对象的缓存
        
        session.close() ;
    }
}


一级缓存,我们不能控制加入缓存数据的数量,当我们查询大量数据时,hibernate会将这些数据加入缓存中,由于缓存占用内存,当数据过大时,可能会造成内存溢出,我们要记得刷新session并清空缓存。

二级缓存: 当我们使用二级缓存时需要在hibernate.cfg.xml中配置缓存的配置项,如下:

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <property name="show_sql">true</property>
    <property name="myeclipse.connection.profile">oracle</property>
    <property name="connection.url">
        jdbc:oracle:thin:@localhost:1521:orcl
    </property>
    <property name="connection.username">scott</property>
    <property name="connection.password">oracle</property>
    <property name="connection.driver_class">
        oracle.jdbc.driver.OracleDriver
    </property>
    <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
    <property name="hbm2ddl.auto">update</property>
    
    <!-- second level cache  -->
    <property name="cache.use_structured_entries">true</property><!-- 启用二级缓存 -->
    <property name="cache.provider_class">org.hibernate.cache.OSCacheProvider</property><!-- 二级缓存的提供类     要加入oscachexx.jar  有很多二级缓存实现类 -->
    <!-- 配置好提供类后    还要添加提供类 有关缓存的配置文件     如:oscache.properties-->
    
    <mapping resource="model/Person.hbm.xml" />
    <class-cache usage="read-only" class="model.Person"/>   <!--配置哪些类使用二级缓存   以及二级缓存的策略       也可以在类配置文件中配置-->
</session-factory>
</hibernate-configuration>


二级缓存的常用提供类有:org.hibernate.cache.EhCacheProvider   要加入ehcache.xml配置文件和ehcache.jar

org.hibernate.cache.OSCacheProvider    要加入oscache.properties配置文件和oscache.jar   

在缓存的配置文件中我们可以配置缓存的有关性质     如:缓存存储对象的个数、是否启用内存缓存、硬盘缓存的存储路径等

配置那个类使用二级缓存,可以在hibernate.cfg.xml中配置如:<class-cache usage="read-only" class="model.Person"/>配置model.Person类使用二级缓存,也可以在类的配置文件中配置:

<class name="Person" table="person">
        <cache usage="read-only"/>  <!--在类配置文件中配置缓存策略--> 
        <id name="id" column="id" type="long">
            <generator class="sequence">
                <param name="sequence">seq_person_id</param>
            </generator>
        </id>
        <property name="name"/>
        <property name="birthdate"/>
    </class>


usage=“read-only”  配置缓存策略 ,有四种:
read-only    :   只读,即使用get、load等获取方法时才将数据添加进二级缓存。    当只读是使用该策略

read-write   :   读写,即读取和修改时都将数据添加进二级缓存。安全,但效率相对较低

nonstrict-read-write : 非严格读写,读写是也添加进二级缓存,但误差大,对数据要求不是太严格的数据可以使用该策略

transactional  : 它保证可重读的事务隔离级别,可以对读/写比例高,很少更新的数据采用该策略。

如下面代码:

@Test
    public void secondLevelCache(){
        Session session = HibernateUtil.getSession() ;
        Transaction transaction = session.beginTransaction() ;
        Person person = new Person() ;
        person.setName("name2") ;
        person.setBirthdate(new Date()) ;
        
        session.save(person) ;     //将person类的二级缓存策略设置为read-only   则只有读取时才加入二级缓存    修改、保存则不加入二级缓存
        transaction.commit() ;
        session.evict(person) ;    //清除一级缓存
        Person person2 = (Person) session.get(Person.class, 2L) ;   //则执行select查询    一级缓存已清除   二级缓存没有该对象
        session.evict(person) ;
        Person person3 = (Person) session.get(Person.class, 2L) ;   //从二级缓存中读取 该对象   
        System.out.println(person3.getName());
        System.out.println(person2.getName()); 
    }


hibernate会管理什么时候加入缓存,什么时候读取缓存,我们要根据对象的使用配置合适的缓存策略。对于那些读/写比例较高的对象可以加入二级缓存,但对于那些经常修改的对象我们最好关闭二级缓存,若打开,不但不能提高性能,由于缓存的负担,可能会更降低系统性能。

oscache.properties 的配置说明:
cache.memory=false     默认true    是否使用内存存储缓存 若为false会缓存到硬盘上     当然我们应该使用内存来存储缓存

cache.capacity=1000    缓存对象的最大数目,  默认不限制   若为负数则不限制

cache.algorithm:缓存的运算规则  为了使用运算规则,则需指定cache.capacity   ,运作规则默认com.opensymphony.oscache.base.algorithm.LRUCache,有三种规则:

             1.    com.opensymphony.oscache.base.algorithm.LRUCache    :last in first out,最迟插入的最先调用。默认值。

             2.   com.opensymphony.oscache.base.algorithm.FIFOCache   :first int first out ,先插入的最先调用。

             3.   com.opensymphony.oscache.base.algorithm.UnlimitedCache :cache中的内容将永远不会被丢弃。若cache.capacity 没有指定size , 则它为默认值

cache.unlimited.disk   :指定硬盘缓存是否要作限制。默认值为false。false的状况下,disk cache capacity 将和cache.capacity的值相同。
cache.persistence.class  :指定的类是被持久化的类。要实现persistenceListeners类。如:使用硬盘持久。可以使用com.opensymphony.oscache.plugins.diskpersistence.HashDiskPersistenceListener,它把class的toString的hash值作为文件名,也可以使用com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener,则会将自生成的字符串作为文件名.使用硬盘持久时我们还应该提供:cache.path来指定硬盘存储的路径。

cache.path 指定硬盘缓存的路径。目录如果不存在将被建立。同时注意oscache应该要有权限写文件系统。 cache.path=c:\\myapp\\cache or *ix: cache.path=/opt/myapp/cache
cache.persistence.overflow.only    :(NEW! Since 2.1) 指定是否只有在内存不足的情况下才使用硬盘缓存。 默认值false。但推荐是true如果内存cache被允许的话。这个属性彻底的改变了cache的行为,使得persisted cache 和memory完全不同。

 cache.blocking 是否同步化。true 或者 false。一般设为true,避免读取脏数据。

cache.event.listeners   :用逗号分离的class名列表。每个class必须实现以下接口之一,或者几个 CacheEntryEventListener:接收cache add/update/flush and remove事件 CacheMapAccessEventListener :接收cache访问事件。这个可以让你跟踪cache怎么工作。 默认是不配置任何class的。当然你可以使用一下的class:      com.opensymphony.oscache.plugins.clustersupport.BroadcastingCacheEventListener -分布式的监听器。可以广播到局域网内的其他cache实例。 com.opensymphony.oscache.extra.CacheEntryEventListenerImpl -一个简单的监听器。在cache的生命周期中记录count of 所有entry的事件。 com.opensymphony.oscache.extra.CacheMapAccessEventListenerImpl -记录count of cache map events(cache hits,misses and state hits).
分布式缓存配置:

cache.cluster.multicast.ip    为广播IP地址   如:cache.cluster.multicast.ip=231.12.21.132 

cache.cluster.properties  :分布式配置属性   如:

  • cache.cluster.properties=UDP(mcast_addr=231.12.21.132;mcast_port=45566;ip_ttl=32;\   
  • mcast_send_buf_size=150000;mcast_recv_buf_size=80000):\   
  • PING(timeout=2000;num_initial_members=3):\   
  • MERGE2(min_interval=5000;max_interval=10000):\   
  • FD_SOCK:VERIFY_SUSPECT(timeout=1500):\   
  • pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800;max_xmit_size=8192):\   
  • UNICAST(timeout=300,600,1200,2400):\   
  • pbcast.STABLE(desired_avg_gossip=20000):\   
  • FRAG(frag_size=8096;down_thread=false;up_thread=false):\   

分布式要配置cache.event.listeners ,当监听缓存,可以使用jm或jgroups来配置消息通知。

缓存也可能操作脏数据的存在,如:当我们操作时将id=1的person加入缓存,有人在后台数据库中修改了person的name字段值,由于该修改没有通知缓存进行更新,缓存中还是原来的数据,我们再获取id=1的person还是从缓存中读取,这个数据就会是错误的。

同样,在服务器分布式系统中,当一台服务器修改了数据库中的数据,这个服务器中的缓存更新了,但其他机器的缓存并没有改变,则其他机器可能出现脏数据,为了解决这个问题,我们就需要使用缓存的分布式处理进行同步。

具体实现还要查询资料。


原文连接:http://www.cnblogs.com/taxuewuhen/archive/2012/08/22/2650107.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值