hibernate的二级缓存

[color=blue]hibernate二级缓存

二级缓存也称进程级的缓存或SessionFactory级的缓存,二级缓存可以被所有的session共享
二级缓存的生命周期和SessionFactory的生命周期一致,SessionFactory可以管理二级缓存[/color]

二级缓存的配置和使用:
* 首先设置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"/>
<!--Default Cache configuration. These will applied to caches programmatically created through
the CacheManager.
The following attributes are required for defaultCache:
maxInMemory - 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.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used the element is not eternal. Idle time is now - last accessed time
imeToLiveSeconds - Sets the time to live for an element before it expires. Is only used if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit.
-->
<defaultCache
maxElementsInMemory="10000" <!-- 缓存最大的对象数目 -->
eternal="false" <!-- 缓存是否持久,不持久就会在规定时间内失效 -->
timeToIdleSeconds="120" <!-- 当缓存闲置n秒后销毁 -->
timeToLiveSeconds="120" <!-- 当缓存存活n秒后销毁-->
overflowToDisk="true" <!-- 是否保存到磁盘,当对象达到规定的10000个时-->
/>
</ehcache>

* 开启二级缓存,修改hibernate.cfg.xml文件
<property name="hibernate.cache.use_second_level_cache">true</property>
* 指定缓存产品提供商,修改hibernate.cfg.xml文件
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
//org.hibernate.cache.OSCacheProvider OSCache的缓存策略提供商
* 指定那些实体类使用二级缓存(两种方法)
* 在映射文件中采用<cache>标签
//写在hibernate.cfg.xml配置文件里面方便管理
<class-cache class="com.bjsxt.hibernate.Student" usage="read-only"/>
* 在hibernate.cfg.xml文件中,采用<class-cache>标签
在ID的前面配置<cache usage="read-only"/>
//usage(必须)说明了缓存的策略: transactional、 read-write、 nonstrict-read-write或 read-only


read-only:如果你的应用程序只需读取一个持久化类的实例,而无需对其修改, 那么就可以对其进行只读 缓存。这是最简单,也是实用性最好的方法。甚至在集群中,它也能完美地运作。

read/write:如果应用程序需要更新数据,那么使用读/写缓存 比较合适。 如果应用程序要求“序列化事务”的隔离级别(serializable transaction isolation level),那么就决不能使用这种缓存策略。 如果在JTA环境中使用缓存,你必须指定hibernate.transaction.manager_lookup_class属性的值, 通过它,Hibernate才能知道该应用程序中JTA的TransactionManager的具体策略。 在其它环境中,你必须保证在Session.close()、或Session.disconnect()调用前, 整个事务已经结束。 如果你想在集群环境中使用此策略,你必须保证底层的缓存实现支持锁定(locking)。Hibernate内置的缓存策略并不支持锁定功能。

其它略过

测试
开启两个session,分别调用load,第二次不会发
开启两个session,分别调用get,第二次不会发

开启两个session,分别调用load,在使用SessionFactory清除二级缓存

//管理二级缓存
SessionFactory factory = HibernateUtils.getSessionFactory();
//factory.evict(Student.class);删除所有的Student对象,下面是只删除一个
factory.evict(Student.class, 1);

当第二次查Student的时候就会发出查询sql,因为二级缓存中的数据被清除了



/**
* 一级缓存和二级缓存的交互
*/
public void testCache4() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();

//仅向二级缓存读数据,而不向二级缓存写数据
session.setCacheMode(CacheMode.GET);
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());

session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}

try {
session = HibernateUtils.getSession();
session.beginTransaction();

//发出sql语句,因为session设置了CacheMode为GET,所以二级缓存中没有数据
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());

session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}

try {
session = HibernateUtils.getSession();
session.beginTransaction();

//只向二级缓存写数据,而不从二级缓存读数据
session.setCacheMode(CacheMode.PUT);

//会发出查询sql,因为session将CacheMode设置成了PUT
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());

session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}

}


CacheMode.NORMAL - 从二级缓存中读、写数据。默认

CacheMode.GET - 从二级缓存中读取数据,仅在数据更新时对二级缓存写数据。

CacheMode.PUT - 仅向二级缓存写数据,但不从二级缓存中读数据。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值