您的位置:首页 > 其它

Hibernate之二级缓存

2015-11-12 10:38 281 查看
摘要: EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。

在讲二级缓存之前先谈谈一级缓存(session):内部缓存
事务范围:一级缓存是Hibernate默认就有的,它只能被当前事务访问,也就是一个session访问,其他session之间不能共享;而Hibernate的二级缓存是受SessionFactory管理的,就是说二级缓存是能够在多个session之间共享的,其生命周期和SessionFactory一致。缓存的生命周期依赖于事务的生命周期,当事务结束时,缓存也就结束生命周期(也就是受session管理,session关闭一级缓存也就结束了)。

一、为什么要用二级缓存?
内行人都知道,我们的应用很多时候都要通过查询物理数据库得到数据返回到客户端,比如淘宝网,我们浏览网站的时候看到的那些数据,但是大家不妨想想,现在全国的网民这么多,比如双11同时访问淘宝主页的人起码都是千万级,而我们暂且不说我们的应用程序,就说一般的数据库每秒的吞吐量根本就吃不消,出现什么后果呢?宕机,也就是数据库崩溃,而我们大家都知道,向淘宝主页的数据一般不会时刻更新,这个时候我们就想能不能通过一次将主页的东西查出来放到某个容器中,然后当再次有人访问主页的时候就不用去查物理数据库了(查询物理数据库耗时间)直接从那个容器中获取数据是不是效率高很多呢?这个时候Hibernate的二级缓存就能大展身手了,它能充当这个容器。最后一句话,用二级缓存就是为了提升系统性能,用有限的硬件资源服务更多的客户。
二、EhCache 的主要特性:
1.快速 2. 简单 3. 多种缓存策略 4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题。三、使用二级缓存的原则: a.数据很少会被修改;b.数据会经常被系统引用;c.该数据大小在可接受范围内;四、二级缓存具体使用步骤:以Hibernate3.2为例 step1:添加相应的jar包,Ehcache.jar和commons-logging.jar,还有Hibernate需要的jar包; step2:配置hibernate.cfg.xml文件,内容如下: <!-- 是否开启二级缓存,true开启二级缓存,false关闭二级缓存 -->
<property name="cache.use_second_level_cache">true</property>
<!-- 开启查询缓存 -->
<property name="cache.use_query_cache">true</property>

<!-- 指定缓存配置文件位置 -->
<property name="cache.provider_configuration_file_resource_path">
ehcache.xml
</property>
<!-- 强制Hibernate以更人性化的格式将数据存入二级缓存 -->
<property name="cache.use_structured_entries">true</property>
<!-- 指明二级缓存的提供商 -->
<property name="cache.provider_class">
org.hibernate.cache.EhCacheProvider
</property>
step3:在src根目录下配置ehcache.xml,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache >
<!--
指定一个目录:当 EHCache 把数据写到硬盘上时, 将把数据写到这个目录下.
-->
<diskStore path="d:\\tempDirectory"/>
<defaultCache
maxElementsInMemory="100" //在该二级缓存中,最多可以缓存对象的数量
eternal="false" //缓存在二级缓存中的对象是否永远都有效,一般配置为false
timeToIdleSeconds="1200" //"钝化":两次访问该二级缓存中对象的,间隔时间
timeToLiveSeconds="1200" //二级缓存中的对象从创建到消亡的时间
overflowToDisk="false"> //如果超过缓存的访问,可以写入到硬盘上的
</defaultCache>
</ehcache>
step4:在相应的实体类映射文件User.hbm.xml中指定缓存策略,内容如下:

<hibernate-mapping>
<class name="com.pojo.Student" table="student" catalog="zhengkuan" dynamic- update="true" >
<!-- 缓存策略 实体只读缓存 -->
<cache usage="read-write"/>
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="math" type="java.lang.Integer">
<column name="math" />
</property>
...
step5:在junit厕所里里面进行测试,代码如下:

public class TestJunit2 {
private SessionFactory sf=null;

@Before
public void setUp() throws Exception {
System.out.println("初始化数据...");
sf = new Configuration().configure().buildSessionFactory();
}

@After
public void tearDown() throws Exception {
System.out.println("释放数据资源...");
}

@Test
public void testEhcache() {
Session session1 = sf.openSession();
Transaction tx = session1.beginTransaction();
Student stu = (Student)session1.get(Student.class, 3);
stu.setEnglish(64);
System.out.println("student1:"+stu);
tx.commit();
session1.close();

Session session2 = sf.openSession();
Transaction tx2 = session2.beginTransaction();
Student stu2 = (Student)session2.get(Student.class, 3);
System.out.println("student2:"+stu);
tx2.commit();
session2.close();
sf.close();

}
...

测试结果如下:只有一条SQL语句说明测试成功了
Hibernate: select student0_.id as id0_0_, student0_.math as math0_0_, student0_.english as english0_0_, student0_.stuName as stuName0_0_, student0_.creatime as creatime0_0_ from zhengkuan.student student0_ where student0_.id=?
student1:Student [id=3, math=86, english=64, stuName=王五, creatime=2015-09-25 00:00:00.0]
student2:Student [id=3, math=86, english=64, stuName=王五, creatime=2015-09-25 00:00:00.0]

备注:Hibernate的get()、load()、iterator()都支持二级缓存读写,list()只支持写入。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息