SSH整合配置二级缓存
一、了解
Hibernate的session提供了一级缓存,每个session,对同一个id进行两次load,不会发送两条sql给数据库,但session关闭时,一级缓存失效。
二级缓存是SessionFactory级别的全局缓存,它底下可以使用不同的缓存类库,比如ehcache、oscache等
对缓存若想进步了解可参考以下网址http://www.360doc.com/content/10/0917/17/2560742_54412898.shtml
二、配置
1、在applicationContext.xml中定义如下:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.configurationResourceName">ehcache.xml</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/crm/model/User.hbm.xml</value>
</list>
</property>
</bean>
2、在src目录下创建ehcache.xml,配置信息如下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="180"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/> <!-- 查询缓存 -->
<cache name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="4200"
overflowToDisk="true">
</cache> <!-- 二级缓存 -->
<cache name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="5000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/> <!-- 给Model设置缓存 -->
<cache name="com.crm.model.User"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="100"
timeToLiveSeconds="4200"
overflowToDisk="true"
/>
</ehcache>
maxElementsInMemory属性用于指定缓存中最多可放多少个对象。
eternal属性指定缓存是否永久有效。
timeToIdleSeconds属性指定缓存多久未被使用便清理掉。
timeToLiveSeconds属性指定缓存的生命长度。
diskPersistent属性指定缓存是否被持久化到硬盘中,保存路径由标签指定。
3、在User.hbm.xml里加上<cache usage="read-write" />,如下图所示

注意:
启动Tomcat,如发现如下错误:
Could not find configuration [org.hibernate.cache.UpdateTimestampsCache]; using defaults.
Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults.
则是第二步没有做,加上即可,配置完毕。
4、执行查询缓存时,若使用Criteria需设置如下(示例):
public List getUserInfoByCondition(Page page) {
List users = null;
Criteria criteria = this.getSession().createCriteria(User.class);
criteria.setFirstResult(page.getBeginIndex());
criteria.setMaxResults(page.getEveryPage());
criteria.setCacheable(true);
users = criteria.list();
return users;
}
至此配置过程完毕。
参考网址:
http://blog.sina.com.cn/s/blog_7cc931cb01013qep.html
http://www.open-open.com/lib/view/open1351002982258.html
SSH整合配置二级缓存的更多相关文章
- Hibernate4+EhCache配置二级缓存
本文主要讲一讲Hibernate+EhCache配置二级缓存的基本使用方法 (有关EhCache的基础介绍可参见:http://sjsky.iteye.com/blog/1288257 ) Cache ...
- (转)为Spring集成的Hibernate配置二级缓存
http://blog.csdn.net/yerenyuan_pku/article/details/52896195 前面我们已经集成了Spring4.2.5+Hibernate4.3.11+Str ...
- mybatis(4)_二级缓存深入_使用第三方ehcache配置二级缓存
增删改对二级缓存的影响 1.增删改也会清空二级缓存 2.对于二级缓存的清空实质上是对value清空为null,key依然存在,并非将Entry<k,v>删除 3.从DB中进行select查 ...
- mybatis整合redis二级缓存
mybatis默认开启了二级缓存功能,在mybatis主配置文件中,将cacheEnabled设置成false,则会关闭二级缓存功能 <settings> <!--二级缓存默认开启, ...
- Hibernate+EhCache配置二级缓存
步骤: 第一步:加入ehcache.jar 第二步: 在src目录下新建一个文件,名为:ehcache.xml 第三步:在hibernate配置文件的<session-factory>下配 ...
- Hibernate4.1.4配置二级缓存EHCache步骤
1.当然首先引入EHCache相关的jar包 这些包不需要另外下载,在Hibernate官方网站下载Hibernate4.1.7的压缩包(如:hibernate-release-4.1.7.Final ...
- springboot 配置二级缓存
springBoot中配置mybatis的二级缓存 2018年01月22日 11:45:37 Ting.Xue(Martin.Xue) 阅读数:5604更多 个人分类: SSM的Spring框架Myb ...
- JBPM4.4+SSH 整合配置及完整实例
整合jBPM4.4+ssh过程(spring接管struts2和hibernate,例中都整合在application.xml中,没有单独的jbpm.hibernate.cfg.xml): 1.在se ...
- java SSH整合配置
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="3 ...
随机推荐
- django inclusion_tag
一种比较普遍的tag类型是只是渲染其它模块显示下内容,这样的类型叫做Inclusion Tag. 例如,实现以下tag: {% books_for_author author %} 渲染结果为: &l ...
- 【OpenStack】OpenStack系列1之Python虚拟环境搭建
安装virtualenv相关软件包 安装:yum install python-virtualenv* -y 简介,安装包主要包括, python-virtualenv:virtualenv用于创建独 ...
- jQuery Ajax 操作函数
jQuery Ajax 操作函数 jQuery 库拥有完整的 Ajax 兼容套件.其中的函数和方法允许我们在不刷新浏览器的情况下从服务器加载数据. 函数 描述 jQuery.ajax() 执行异步 H ...
- Ninject学习笔记<二>
本文转载自kuangkro 如果给您带来不便请联系博主 一.控制反转和依赖注入 Ninject是一个轻量级的基于.Net平台的依赖注入(IOC)框架.所谓的IOC,即控制反转(Inversion of ...
- 20. 最长公共子串(ToDo)[LCS]
[参考] http://zhedahht.blog.163.com/blog/static/254111742007376431815/
- 通过Java反射来理解泛型的本质
集合框架中经常会使用泛型指定集合中所存放元素的类型,保证集合的统一性,从集合中取出元素的时候也避免了类型强制转换的操作,所以我们使用常规的方式来往集合中存放元素的时候,如果指定泛型,那么我们只能向集合 ...
- Java中的内存分配机制
Java的内存分为两种:一种是栈内存,一种是堆内存. 在函数中定义的一些基本类型变量和对象的引用都在函数的栈内存中分配.当在一个代码块中定义一个变量的时候,java就在栈中为其分配内存,当超过作用域的 ...
- OC内存管理--zombie对象
当我们对于内存进行手动管理的时候,会出现两种错误:一种是野指针错误,一种则为内存泄露.这两点也是我们去管理内存时最终要解决的问题. 内存泄漏是指:不在使用的对象,一直保留在内存中未被销毁,一直占有着内 ...
- IDE整理
1.eclipse 下载地址:http://www.eclipse.org/downloads/ 2.myeclipse 下载地址:http://www.myeclipseide.com/mo ...
- 如何从Apache官网下载windows版apache服务器
参考文章:http://jingyan.baidu.com/article/29697b912f6539ab20de3cf8.html