1、首先设置EhCache,建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000" <!-- 缓存最大数目 -->
eternal="false" <!-- 缓存是否持久 -->
overflowToDisk="true" <!-- 是否保存到磁盘,当系统当机时-->
timeToIdleSeconds="300" <!-- 当缓存闲置n秒后销毁 -->
timeToLiveSeconds="180" <!-- 当缓存存活n秒后销毁-->
diskPersistent="false"
diskExpiryThreadIntervalSeconds= "120"/>
</ehcache>
2、在Hibernate
配置文件中设置:
<!-- 设置Hibernate
的缓存接口类,这个类在Hibernate
包中 -->
<property name="cache.provider_class">org.hibernate
.cache.EhCacheProvider</property>
<!-- 是否使用查询缓存 -->
<property name="hibernate
.cache.use_query_cache">true</property>
如果使用spring调用Hibernate
的sessionFactory的话,这样设置:
<!--Hibernate
Session工厂管理 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate
3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="datasource" />
</property>
<property name="hibernate
Properties">
<props>
<prop key="hibernate
.dialect">org.hibernate
.dialect.Oracle9Dialect</prop>
<prop key="connection.provider_class">org.hibernate
.connection.C3P0ConnectionProvider</prop>
<prop key="hibernate
.show_sql">true</prop>
<prop key="hibernate
.cache.use_query_cache">true</prop>
<prop key="hibernate
.cache.provider_class">org.hibernate
.cache.EhCacheProvider</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>/WEB-INF/classes/cn/rmic/manager/hibernate
/</value>
</list>
</property>
</bean>
说明一下:如果不设置“查询缓存”,那么hibernate
只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置
hibernate
.cache.use_query_cache true 才行
3、在Hbm文件中添加<cache usage="read-only"/>
4、如果需要“查询缓存”,还需要在使用Query或Criteria()时设置其setCacheable(true);属性
5、实践出真知,给一段测试程序,如果成功的话第二次查询时不会读取数据库
package cn.rmic.hibernate
sample;
import java.util.List;
import org.hibernate
.CacheMode;
import org.hibernate
.Criteria;
import org.hibernate
.Query;
import org.hibernate
.Session;
import cn.rmic.hibernate
sample.hibernate
.Hibernate
SessionFactory;
import cn.rmic.manager.po.Resources;
public class testCacheSelectList ...{
/** *//**
* @param args
*/
public static void main(String[] args) ...{
// TODO Auto-generated method stub
Session s=Hibernate
SessionFactory.getSession();
Criteria c=s.createCriteria(Resources.class);
c.setCacheable(true);
List l=c.list();
// Query q=s.createQuery("From Resources r")
// .setCacheable(true)
// .setCacheRegion("frontpages") ;
// List l=q.list();
Resources resources=(Resources)l.get(0);
System.out.println("-1-"+resources.getName());
Hibernate
SessionFactory.closeSession();
try ...{
Thread.sleep(5000);
} catch (InterruptedException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
s=Hibernate
SessionFactory.getSession();
c=s.createCriteria(Resources.class);
c.setCacheable(true);
l=c.list();
// q=s.createQuery("From Resources r").setCacheable(true)
// .setCacheRegion("frontpages");
// l=q.list();
resources=(Resources)l.get(0);
System.out.println("-2-"+resources.getName());
Hibernate
SessionFactory.closeSession();
}
}
分享到:
相关推荐
Hibernate+ehcache二级缓存技术 Hibernate+ehcache二级缓存技术
struts2+hibernate+ehcache二级缓存的 步骤和需要的代码及jar包
此配置和包,是springMVC4.3.3 +spring4+hibernate5.1.3+二级缓存ehcache(不用可以关闭)+fastjson。 是正常运行的项目里拷出来的,方便大家使用。
Hibernate EhCache 二级缓存配置
hibernate使用的一些例子,二级缓存ehcache的使用 一些设计模式的demo代码
ehcache 二级缓存 配置使用的jar包 配置如下: <!-- 启用二级缓存 --> <property name="hibernate.cache.use_second_level_cache">true <!-- 查询的二级缓存配置 --> <property name="hibernate....
配置 EhCache 的实例 1、 简介: EhCache缓存插件是Hibernate的另一个项目,Hibernate框架内置了对它 的支持,对于单机应用推荐使用它做为Hibernate的二级缓存。
hibernate4配置ehcache二级缓存,以及相关的jar都在源码中。
SSH三层架构MVC(struts1.3+spring2.x+hibernate3.2),Hibernate(ehcache)二级缓存技术,Spring 注解形式依赖注入,ehcache缓存 源代码,内有MySql anbyke.sql文件,方便创建数据库演示效果!
struts2+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,mysql数据,需要自己创建
Hibernate中默认的二级缓存机制ehcache案例,属性ehcache环境搭建,配置问题。
ehcache全解析 利用Spring和EHCache缓存结果 Hibernate+ehcache二级缓存技术 Spring基于注解的缓存配置--web应用实例 http://zjava.org.ru/
struts1.x+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,数据库mysql,需要自己创建
Hibernate4二级缓存Ehcache案例,可参考博客:http://blog.csdn.net/coco2d_x2014/article/details/52927638
本人在做项目时用到了Hibernate的二级缓存,使用的是EhCache,结合本人自己的理解总结了如何在java web项目中配合Hibernate使用二级缓存,以提高程序的性能,附带需要的文件,参考的文件,和测试类以及说明。
hibernate二级缓存 ehcache
使用Java工程实现了Hibernate二级缓存(Ehcache),其中还有自己封装的一个jar包实现了简单的分页功能
2.4. 使用EhCache配置二级缓存: 2.4.1. 配置准备: 1) 把ehcache-1.2.3.jar加入到当前应用的classpath中。 2) 在hibernate.cfg.xml文件中加入EhCache缓存插件的提供类。 <!--配置缓存插件 --> ...
Hibernate二级缓存+分页功能
hibernate二级缓存相关插件(EHcache)