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 ...
随机推荐
- Rotate String
Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...
- Android Studio在线安装Android SDK注意事项
由于使用的Android studio自带了sdk23,然而其它版本的sdk并没有安装:这些天由于需要用到低版本的sdk,因而使用Android SDK Manager进行相应的更新.开始的时候老是无 ...
- Android Services重点记录
今天阅读了google的官方文档 Services,对重点做下记录. 首先,Services默认运行在主线程中,所以一般情况下,要手动创建一个thread. 系统除了Services,还为我们提供了一 ...
- mysqldump备份
备份工具1.mysqldump(数据量很大时不推荐使用) myisam 锁表 innodb 行锁 mysqldump --help | less #查看mysql所有的语法 mysqldu ...
- Java问题排查工具箱[转载]
转载自:http://hellojava.info/?p=517 作者:阿里毕玄 问题排查除了最重要的解决思路和逻辑推导能力外,工具也是不可缺少的一部分,一个好用的工具可以事半功倍,甚至在某些情况下会 ...
- FreeMarker备忘
以下内容全部是网上收集: FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主要由如下4个部分组成: ,文本:直接输出的部分 ,注释:<#-- ... --& ...
- Man简单介绍
转自:http://os.51cto.com/art/201312/425525.htm Linux系统提供了相对比较丰富的帮助手册(man),man是manual的缩写,在日常linux系统管理中经 ...
- Android Bander设计与实现 - 设计篇
转自:http://blog.csdn.net/universus/article/details/6211589#t7 Binder Android IPC Linux 内核 驱动 摘要 Binde ...
- hdu 1286:找新朋友(数论,欧拉函数)
找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- PHP+socket+SMTP、POP3协议发送、接收邮件
.实现SMTP协议的类dsmtp.cls.php:<?php , $webname=).); } } .实现POP3协议的类dpop3.cls.php: <? ...