1、前言
二级缓存是属于SessionFactory级别的缓存机制,是属于进程范围的缓存。一级缓存是Session级别的缓存,是属于事务范围的缓存,由Hibernate管理,一般无需进行干预。
Hibernate支持以下的第三方的缓存框架:
Cache | Interface | Supported strategies | ||
---|---|---|---|---|
HashTable (testing only) |
| |||
EHCache |
| |||
OSCache |
| |||
SwarmCache |
| |||
JBoss Cache 1.x |
| |||
JBoss Cache 2.x |
|
2、下载第三方ehcache.jar
ehcache.jar包的话,有两种,一种是org.ehcache,另一种是net.sf.ehache。Hibernate集成的是net.sf.ehcache!!所以应该下载net.sf.ehcache。如果使用org.ehcache的jar包,hibernate是不支持的!!
下载下来的jar包,需要放到项目当中,在本案例,是放到SSHWebProject项目的WebRoot/WEB-INF/lib目录下,需要注意的是,ehcache.jar包依赖commons-logging.jar,你还得看看你项目中有没有commons-logging.jar!!
3、开启hibernate的二级缓存
想是否使用hibernate.cfg.xml配置文件,会导致配置使用二级缓存的方式不一样,一般是由于项目集成了Spring框架,所以配置二级缓存的话,
就分以下两种情况:
1)项目有hibernate.cfg.xml
1-1)开启Hibernate缓存二级缓存功能
修改hibernate.cfg.xml,添加以下内容
<!-- 开启二级缓存,使用EhCache缓存 -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
1-2)配置哪些实体类的对象需要存放到二级缓存,有两种方式
本案例中,以edu.po.Users用户对象为例子,对应的实体映射文件为Users.hbm.xml。
方式一,在hibernate.cfg.xml文件中配置
<!-- 要使用二级缓存的对象配置 -->
<class-cache usage="read-write" class="edu.po.Users"/>
<!-- 缓存集合对象的例子 -->
<!-- <collection-cache usage="read-write" collection="cn.itcast.domain.Customer.orders"/> -->
方式二,在hbm文件(实体映射文件)中配置
Users.hbm.xml,添加以下内容:
<cache usage="read-write"/>
2)集成了Spring框架之后,没有hibenate.cfg.mlx文件
1-1)开启Hibernate缓存二级缓存功能
修改applicationContext.xml文件,在sessionFactory Bean中添加以下hibernateProperties,如下:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
......
<property name="hibernateProperties">
<props>
......
<!-- 开启二级缓存,使用EhCache缓存 -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
......
</props>
</property>
<property name="mappingResources">
<list>
<value>edu/po/Users.hbm.xml</value>
<value>edu/po/TLog.hbm.xml</value>
</list>
</property>
</bean>
1-2)配置哪些实体类的对象需要存放到二级缓存
本案例中,以edu.po.Users用户对象为例子,对应的实体映射文件为Users.hbm.xml。
在hbm文件(实体映射文件)中配置
Users.hbm.xml,添加以下内容:
<cache usage="read-write"/>
4、配置ehcache.xml文件
将ehcache.jar包中的ehcache-failsafe.xml 改名为 ehcache.xml 放入 src,因为hibernate默认找classpath*:ehcache.xml
本案例,对<diskStore>和<defaultCache>进行修改,如下
<diskStore path="d:/EhCacheData"/>
<!--
maxElementsInMemory: 内存中最大对象数量 ,超过数量,数据会被缓存到硬盘
eternal:缓存的对象是否有有效期(即是否永久存在)。如果为true,timeouts属性被忽略
timeToIdleSeconds:缓存的对象在过期前的空闲时间,单位秒
timeToLiveSeconds:存活时间,对象不管是否使用,到了时间回收,单位秒
overflowToDisk:当内存中缓存对象数量达到 maxElementsInMemory 限制时,是否可以写到硬盘
maxElementsOnDisk:硬盘缓存最大对象数量
diskPersistent:在java虚拟机(JVM)重启或停掉的时候,是否持久化磁盘缓存,默认是false
diskExpiryThreadIntervalSeconds:清除磁盘缓存中过期对象的监听线程的运行间隔,默认是120秒
memoryStoreEvictionPolicy:当内存缓存的对象数量达到最大,有新的对象要加入的时候,
移除缓存中对象的策略。默认是LRU,可选的有LFU和FIFO
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
5、demo
SpringBeanUtils.java:
package utils;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* Title: SpringBeanUtils.java
* Description: 获取Spring的Bean实例对象的工具类
* @author yh.zeng
* @date 2017-6-27
*/
public class SpringBeanUtils {
private static Logger logger = Logger.getLogger(SpringBeanUtils.class);
static String filePath ="WebRoot/WEB-INF/applicationContext.xml";
static ApplicationContext CONTEXT ;
static{
try{
CONTEXT = new FileSystemXmlApplicationContext(filePath);
}catch(Exception e){
logger.error(StringUtils.getExceptionMessage(e));
}
}
/**
* 获取Bean
* @param uniqueIdentifier Bean的唯一标识,可以是ID也可以是name
* @return
*/
public static Object getBean(String uniqueIdentifier){
return CONTEXT.getBean(uniqueIdentifier);
}
/**
* 获取SessionFacotry对象
* @param uniqueIdentifier SessionFactory Bean的唯一标识,可以是ID也可以是name
* @return
*/
public static SessionFactory getSessionFactory(String uniqueIdentifier){
return (SessionFactory) CONTEXT.getBean(uniqueIdentifier);
}
public static String getFilePath() {
return filePath;
}
public static void setFilePath(String filePath) {
SpringBeanUtils.filePath = filePath;
CONTEXT = new FileSystemXmlApplicationContext(filePath);
}
}
HibernateEhCacheTest.java:
注意:查询缓存和查看Cache(缓存)统计信息的功能,需要做另外配置,见博客Hibernate开启查询缓存、Hibernate开启收集缓存统计信息
package edu.test;
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.stat.EntityStatistics;
import org.hibernate.stat.Statistics;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import utils.SpringBeanUtils;
import edu.po.Users;
/**
* Title: HibernateEhCacheTest.java
* Description: Hibernate二级缓存测试
* @author yh.zeng
* @date 2017-7-4
*/
public class HibernateEhCacheTest {
static SessionFactory sessionFactory = SpringBeanUtils.getSessionFactory("sessionFactory");
public static void main(String args[]) {
//Session.get()方法支持二级缓存
System.out.println("###############session.get()###############");
Session session1 = sessionFactory.openSession();
session1.beginTransaction();
Users user1 = (Users)session1.get(Users.class, 6);
System.out.println("用户名:" + user1.getUsername());
session1.getTransaction().commit();
session1.close();
Session session2 = sessionFactory.openSession();
session2.beginTransaction();
Users user2 = (Users)session2.get(Users.class, 6);
System.out.println("用户名:" + user2.getUsername());
session2.getTransaction().commit();
session2.close();
//Query.list()方法支持查询缓存
System.out.println("###############Query.list()###############");
Session session3 = sessionFactory.openSession();
session3.beginTransaction();
Query query = session3.createQuery("from Users where id = :id");
query.setParameter("id", 6);
query.setCacheable(true); //启用查询缓存
Users user3 = (Users)query.list().get(0);
System.out.println("用户名:" + user3.getUsername());
session3.getTransaction().commit();
session3.close();
Session session4 = sessionFactory.openSession();
session4.beginTransaction();
Query query2 = session4.createQuery("from Users where id = :id");
query2.setParameter("id", 6);
query2.setCacheable(true); //启用查询缓存
Users user4 = (Users)query2.list().get(0);
System.out.println("用户名:" + user4.getUsername());
session4.getTransaction().commit();
session4.close();
//Query.iterate()方法不支持查询缓存
System.out.println("###############Query.iterate()###############");
Session session5 = sessionFactory.openSession();
session5.beginTransaction();
Query query3 = session5.createQuery("from Users where id = :id");
query3.setParameter("id", 6);
query3.setCacheable(true); //启用查询缓存
Users user5 = (Users)query3.iterate().next();
System.out.println("用户名:" + user5.getUsername());
session5.getTransaction().commit();
session5.close();
Session session6 = sessionFactory.openSession();
session6.beginTransaction();
Query query4 = session6.createQuery("from Users where id = :id");
query4.setParameter("id", 6);
query4.setCacheable(true); //启用查询缓存
Users user6 = (Users)query4.iterate().next();
System.out.println("用户名:" + user6.getUsername());
session6.getTransaction().commit();
session6.close();
//Session.load()方法支持二级缓存
System.out.println("###############Session.load()###############");
Session session7 = sessionFactory.openSession();
session7.beginTransaction();
Users user7 = (Users)session7.load(Users.class, 6);
System.out.println("用户名:" + user7.getUsername());
session7.getTransaction().commit();
Session session8 = sessionFactory.openSession();
session8.beginTransaction();
Users user8 = (Users)session8.load(Users.class, 6);
System.out.println("用户名:" + user8.getUsername());
session8.getTransaction().commit();
//HibernateTemplate.find支持查询缓存
System.out.println("###############HibernateTemplate.find()###############");
HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);
hibernateTemplate.setCacheQueries(true); //开启查询缓存
Users user9 = (Users)hibernateTemplate.find("from Users where id = ?",6).get(0);
System.out.println("用户名:" + user9.getUsername());
HibernateTemplate hibernateTemplate2 = new HibernateTemplate(sessionFactory);
hibernateTemplate2.setCacheQueries(true); //开启查询缓存
Users user10 = (Users)hibernateTemplate2.find("from Users where id = ?",6).get(0);
System.out.println("用户名:" + user10.getUsername());
//HibernateTemplate.execute支持查询缓存
System.out.println("###############HibernateTemplate.execute()###############");
HibernateTemplate hibernateTemplate3 = new HibernateTemplate(sessionFactory);
hibernateTemplate3.setCacheQueries(true); //开启查询缓存
Users user11 = hibernateTemplate3.execute(new HibernateCallback<Users>() {
@Override
public Users doInHibernate(Session session) throws HibernateException,
SQLException {
// TODO Auto-generated method stub
Query query = session.createQuery("from Users where id = :id");
query.setParameter("id", 6);
return (Users)query.list().get(0);
}
});
System.out.println("用户名:" + user11.getUsername());
HibernateTemplate hibernateTemplate4 = new HibernateTemplate(sessionFactory);
hibernateTemplate4.setCacheQueries(true); //开启查询缓存
Users user12 = hibernateTemplate4.execute(new HibernateCallback<Users>() {
@Override
public Users doInHibernate(Session session) throws HibernateException,
SQLException {
// TODO Auto-generated method stub
Query query = session.createQuery("from Users where id = :id");
query.setParameter("id", 6);
return (Users)query.list().get(0);
}
});
System.out.println("用户名:" + user12.getUsername());
//Cache统计统计信息
Statistics statistics= sessionFactory.getStatistics();
System.out.println(statistics);
System.out.println("放入"+statistics.getSecondLevelCachePutCount());
System.out.println("命中"+statistics.getSecondLevelCacheHitCount());
System.out.println("错过"+statistics.getSecondLevelCacheMissCount());
//详细的Cache统计信息
for (int i = 0; i < statistics.getEntityNames().length; i++) {
String entityName = statistics.getEntityNames()[i];
EntityStatistics entityStatistics = statistics.getEntityStatistics(entityName);
StringBuilder cacheOperator = new StringBuilder();
cacheOperator.append("CategoryName:" ).append(entityStatistics.getCategoryName())
.append(",DeleteCount:").append(entityStatistics.getDeleteCount())
.append(",FetchCount:").append(entityStatistics.getFetchCount())
.append(",InsertCount:").append(entityStatistics.getInsertCount())
.append(",LoadCount:").append(entityStatistics.getLoadCount())
.append(",OptimisticFailureCount:").append(entityStatistics.getOptimisticFailureCount())
.append(",UpdateCount:").append(entityStatistics.getUpdateCount());
System.out.println(cacheOperator.toString());
}
}
}
项目demo: https://github.com/zengyh/SSHWebProject.git