Hibernate学习笔记(六)

我们知道hibernate的一级缓存是将数据缓存到了session中从而减少与数据库的交互。那么二级缓存呢?

一、应用场合

比如,在12306购票时,需要选择出发地与目的地,,如果每点一次都与数据库交互一次,这就很不合适,这些地点数据在相当长的一段时间内是不会发生变化的(山东省在相当长的时间内还叫山东省),所以应该缓存起来,没必要每次都与数据库交互,而且该类数据安全性也不是很高。

适合二级缓存的数据:

在现代软件开发中,确实存在一类数据没有什么私有性,为公开的数据,数据基本上不发生变化,该数据保密性不是很强,但又会经常被用到(比如火车票上的出发地与目的地数据)。

注意:如果一个数据一直在改变,不适合用缓存。

流式数据:数据时时刻刻在变的数据,比如手机应用获取手机所在地,后台的推送系统,发出一些信息,比如短信会提示你某天夜间流量超过多少,建议购买夜间流量。而这类数据适合使用strom来处理 。

二、生命周期

二级缓存为sessionFactory级别的缓存,其生命周期和sessionFactory是一致的,Hibernate启动后就有了.

二级缓存在Hibernate中的位置

所以Hibernate内部并没有实现二级缓存,而是应用第三方插件来实现二级缓存的。

三、二级缓存的设置

利用的是ehcache实现的二级缓存

1.添加ehcache所需的jar包

2.在hibernate的配置文件中进行配置

四、二级缓存的操作

哪些方法可以把对象放入到二级缓存中?

get方法,list方法可以把一个或者一些对象放入到二级缓存中

哪些方法可以把对象从二级缓存中提取出来?

get方法,iterator方法可以提取

注意:用的时候一定要小心使用各方法,取数据时,如果把list用成iterate会造成效率的及其降低

五、二级缓存的存储策略

read-only:对象只要加载到二级缓存以后,就只能读取,不能修改。

read-write:对二级缓存中的对象能够进行读和写的操作

注意:一般都设置为read-only

缓存得到磁盘

如果一个系统的权限特别大,这就不适合长时间的放入到二级缓存中,会导致占用内存逐渐变大,查询效率逐渐降低,这种情况可以二级缓存移到磁盘上,但是在现代开发如果需要缓存较多,一般都是使用分布式缓存。

测试:

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN"""><hibernate-configuration><session-factory><property name="hibernate.dialect"><![CDATA[org.hibernate.dialect.MySQLDialect]]></property><property name="hibernate.connection.driver_class"><![CDATA[com.mysql.jdbc.Driver]]></property><property name="hibernate.connection.url"><![CDATA[jdbc:mysql:///hibernate1]]></property><property name="hibernate.connection.username"><![CDATA[root]]></property><property name="hibernate.connection.password"><![CDATA[qiaolezi]]></property><property name="hibernate.c3p0.max_size">20</property><property name="hibernate.c3p0.min_size">10</property><property name="hibernate.c3p0.max_statements">10</property><property name="hibernate.c3p0.acquire_increment">5</property><property name="hibernate.c3p0.timeout">3000</property><!– 二级缓存供应商 –><property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property><!– 开启二级缓存 –><property name="cache.use_second_level_cache">true</property><!– 开启二级缓存的统计机制 –><property name="generate_statistics">true</property><property name="hbm2ddl.auto">update</property><property name="show_sql">true</property><property name="current_session_context_class">thread</property><property name="format_sql">true</property><mapping resource="cn/cil/domain/Classes.hbm.xml" /><mapping resource="cn/cil/domain/Student.hbm.xml" /></session-factory></hibernate-configuration>Classes

public class Classes implements Serializable{private Long cid;private String name;private Set<Student> students;}Classes.hbm.xml

告诉自己,我这次失败了,重新开始吧!下次我会吸取教训,不让自己犯同样的错误的

Hibernate学习笔记(六)

相关文章:

你感兴趣的文章:

标签云: