hibernate二级缓存
二级缓存称为进程级缓存或SessionFactory级缓存,它可以被所有session共享,它的生命周期伴随着SessionFactory的生命周期存在和消亡。
第一步:复制ehcache.xml文件到src目录下,配置二级缓存
<defaultCache
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds和timeToLiveSeconds就没有意义了
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskPersistent="true"
overflowToDisk="true"
/>
也可以配置某个特定对象的,如(但一般情况是使用缺省的就可以了):
<cache name="uuser"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
第二步:启用二级缓存
1.启用二级缓存,默认是启用的:
<property name="cache.use_second_level_cache">true</property>
2.设置缓存产品,如:
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
3.指定哪些类使用二级缓存(两种方法):
a.在hibernate.cfg.xml配置文件中定义
如:<class-cache class="lovo.po.User" usage="read-write"/>
b.在映射文件中定义
如:在<class>节点下:<cache usage="read-only"/>
有些类设置二级缓存,有些类不设置二级缓存;所以如果在映射文件中定义二级缓存的话会导致<cache>标签遍布在文件中,
因此常用第一种,即在hibernate.cfg.xml配置文件中定义,这样便于维护和管理。
第三步:二级缓存实例
1.打开两次session,调用load查询
代码部分:
Session session = this.sf.openSession();
User u1 = (User) session.load(User.class, 1);
System.out.println("用户姓名:"+u1.getName());
//关闭seeion,释放一级缓存
session.close();
//重新开启session
session = this.sf.openSession();
User u = (User) session.load(User.class, 1);
System.out.println("用户姓名:"+u.getName());
session.close();
执行部分:
如果设置<property name="cache.use_second_level_cache">false</property>,执行结果如下:
Hibernate:
select
user0_.id as id0_0_,
user0_.name as name0_0_,
user0_.password as password0_0_,
user0_.createTime as createTime0_0_,
user0_.expireTime as expireTime0_0_
from
t_user user0_
where
user0_.id=?
用户姓名:wangwu
Hibernate:
select
user0_.id as id0_0_,
user0_.name as name0_0_,
user0_.password as password0_0_,
user0_.createTime as createTime0_0_,
user0_.expireTime as expireTime0_0_
from
t_user user0_
where
user0_.id=?
用户姓名:wangwu
如果设置<property name="cache.use_second_level_cache">true</property>,执行结果如下:
Hibernate:
select
user0_.id as id0_0_,
user0_.name as name0_0_,
user0_.password as password0_0_,
user0_.createTime as createTime0_0_,
user0_.expireTime as expireTime0_0_
from
t_user user0_
where
user0_.id=?
用户姓名:wangwu
用户姓名:wangwu
可见,二级缓存是起作用了的。用get测试是同样的结果
2.在1的代码中加入this.sf.evict(User.class),清除二级缓存中的对象,执行结果就跟设置了<property name="cache.use_second_level_cache">false</property>的效果相同。
当然也可以指定清除哪些对象:this.sf.evict(User.class,1),结果很简单。
3.一级缓存和二级缓存的交互(CacheMode:CacheMode.NORMAL、CacheMode.PUT、CacheMode.GET)
代码部分:
Session session = this.sf.openSession();
//CacheMode.GET只向二级缓存中读数据不写
session.setCacheMode(CacheMode.GET);
User u1 = (User) session.load(User.class, 1);
//代码1和代码2顺序不能颠倒,否则CacheMode.GET对1号用户不起作用
System.out.println("用户姓名:"+u1.getName());
session.close();
//重新开启session
session = this.sf.openSession();
User u = (User) session.load(User.class, 1);
System.out.println("用户姓名:"+u.getName());
session.close();
执行结果:
Hibernate:
select
user0_.id as id0_0_,
user0_.name as name0_0_,
user0_.password as password0_0_,
user0_.createTime as createTime0_0_,
user0_.expireTime as expireTime0_0_
from
t_user user0_
where
user0_.id=?
用户姓名:wangwu
Hibernate:
select
user0_.id as id0_0_,
user0_.name as name0_0_,
user0_.password as password0_0_,
user0_.createTime as createTime0_0_,
user0_.expireTime as expireTime0_0_
from
t_user user0_
where
user0_.id=?
用户姓名:wangwu
默认情况下又读又写,即为CacheMode.NORMAL
CacheMode.PUT只写不读
- 默认分类(20)
- J2EE(25)
- Java(56)
- PHP(55)
- SEO(10)
- 网页设计(20)
- 网站建设(37)
- 数据库(7)
- JavaScript(17)
- JQuery(6)
- MySQL(20)
- SQL Server(6)
- Access(1)
- Oracle(6)
- office(6)
- Dreamweaver(4)
- Photoshop(12)
- Flash(9)
- Fireworks(13)
- CSS(14)
- HTML(4)
- .NET(7)
- ASP(2)
- DB2(1)
- Ajax(2)
- Linux(12)
- Struts(7)
- Hibernate(8)
- Spring(2)
- Jsp(22)
- Asp(8)
- C#(3)
- C++(1)
- 网络安全(5)
- 软件工程(7)
- XML(1)
- English(2)
- 计算机等级考试(2)
- 计算机病毒(4)
- 个人日志(76)
- 互联网(15)
- ActionScript(10)
- Android(3)
- 数据结构与算法(1)
- 游戏策略(3)
- 美文翻译(2)
- 编程开发(19)
- 计算机应用(4)
- 计算机(10)
- Unity3d(6)
- 其他(1)
- egret(1)