Hibernate本身不提供二级缓存,所以需要使用第三方插件来作为二级缓存;本次使用memcached作为Hiberbate的二级缓存:添加步骤如下:

一、需要安装memcached服务端

1. 下载memcached的windows稳定版,

(本次测试使用下载memcached http://www.newasp.net/soft/63735.html
2. 在CMD下进入memcached解压路径:输入 memcached.exe -d install" 安装.

3.
再输入:memcached.exe -d start" 启动,可以看到进程中多了memcached。

NOTE:1、
一定要开启memcached服务;2、以后memcached将作为windows的一个服务每次开机时自动启动。

二、导包

commons-codec-1.3.jar

hibernate-memcached-1.2.2.jar(很多文章中少了这个,导致失败!)

memcached-2.1.jar

spy-2.4.jar

slf4j-api-1.5.0.jar

三、修改配置文件

1、applicationContext.xml

 <property name="hibernateProperties">
<!-- hibernate memcached 二级缓存 -->
<value>
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.query.substitutions=true 1, false 0
hibernate.jdbc.batch_size=20<!--默认的数据库-->
<!--配置Hibernate使用cache提供类-->
hibernate.cache.provider_class=com.googlecode.hibernate.memcached.MemcachedCacheProvider
<!-- 开启Hibernate的二级缓存 -->
hibernate.cache.use_second_level_cache=true
<!--设置查询缓存开启-->
hibernate.cache.use_query_cache=true
<!-- 设置memcached缓存服务器的端口 -->
hibernate.memcached.servers=localhost:11211
<!-- 设置二级缓存的前缀名称 -->
hibernate.cache.region_prefix=quality.cache.ehcache
<!-- 否使用结构化的方式缓存对象 -->
hibernate.cache.use_structured_entries=true
<!-- 操作超时时间设置,单位ms -->
hibernate.memcached.operationTimeout=300000
<!-- 缓存失效时间,单位秒 -->
hibernate.memcached.cacheTimeSeconds=300
</value>
</property>

更多详细的参数请参考http://code.google.com/p/hibernate-memcached/wiki/Configuration

此时启动tomcat服务器,console假如有如下的信息,

2013-07-20 18:29:42.426 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-07-20
18:29:42.426 INFO net.spy.memcached.MemcachedConnection:  Connection state
changed for sun.nio.ch.SelectionKeyImpl@8c4549

那恭喜您,您已经成功配置memcached作为二级缓存了!但是此时还不能使用memcached。

2、修改数据表映射文件*.hbm.xml

只需在需要缓存的字段前加上<cache
usage="read-write"/>,根据不同的需求,usage可改为read、write

<hibernate-mapping package="com.xxx.xxx.xxx">
<class name="xxx" table="xxx_xxx">
<cache usage="read-only" />
<id name="id">
<generator class="native" />
</id>
<property generated="never" lazy="false" name="title" />
<property generated="never" lazy="false" length="50000" name="content"
type="text" />
<property generated="never" lazy="false" name="faceIcon" />
<property generated="never" lazy="false" name="postTime"
type="timestamp" />
<property generated="never" lazy="false" name="ipAddr" />
3、查询语句前

加上query.setCacheable(true);//这样才能使查询的时候调用缓存!要对查询设计到的所有方法添加开启二级缓存

OK,此时应该是可以使用memcached作为强大的二级缓存了!

四、案例

第一次点击搜索按钮,console输出信息为

Hibernate:
select
department0_.id as id21_0_,
department0_.name as name21_0_,
department0_.principal as principal21_0_,
department0_.description as descript4_21_0_,
department0_.parentId as parentId21_0_
from
suma_department department0_
where
department0_.id=?
Hibernate:
select
department0_.id as id21_0_,
department0_.name as name21_0_,
department0_.principal as principal21_0_,
department0_.description as descript4_21_0_,
department0_.parentId as parentId21_0_
from
suma_department department0_
where
department0_.id=?

第二、三、四...次点击搜索按钮,console输出信息为:

就没有sql语句的打印信息:页面依旧正常能查询到数据,说明memcached二级缓存添加成功

Ok,第一次之后的查询在缓存生命周期内都不用到数据库去取数据了。

骚年:已经成功了,还等什么?

参考地址忘记了 :-----

SSH整合缓存之-Memcached作为hibernate的二级缓存的更多相关文章

  1. Hibernate的二级缓存策略

    Hibernate的二级缓存策略的一般过程如下: 1) 条件查询的时候,总是发出一条select * from table_name where …. (选择所有字段)这样的SQL语句查询数据库,一次 ...

  2. Hibernate的二级缓存使用(spring使用)

    (一)Hibernate的二级缓存策略的一般过程如下: 1) 条件查询的时候,总是发出一条select * from table_name where …. (选择所有字段)这样的SQL语句查询数据库 ...

  3. ssh整合hibernate 使用spring管理hibernate二级缓存,配置hibernate4.0以上二级缓存

    ssh整合hibernate 使用spring管理hibernate二级缓存,配置hibernate4.0以上二级缓存 hibernate  : Hibernate是一个持久层框架,经常访问物理数据库 ...

  4. 【SSH网上商城项目实战16】Hibernate的二级缓存处理首页的热门显示

    转自:https://blog.csdn.net/eson_15/article/details/51405911 网上商城首页都有热门商品,那么这些商品的点击率是很高的,当用户点击某个热门商品后需要 ...

  5. hibernate(九) 二级缓存和事务级别详讲

    序言 这算是hibernate的最后一篇文章了,下一系列会讲解Struts2的东西,然后说完Struts2,在到Spring,然后在写一个SSH如何整合的案例.之后就会在去讲SSM,在之后我自己的个人 ...

  6. Java面试题:Hibernate的二级缓存与Hibernate多表查询

    我们来看两个有关Java框架之Hibernate的面试题,这是关于Hibernate的常考知识点. 1.请介绍一下Hibernate的二级缓存 解题按照以下思路来回答: (1)首先说清楚什么是缓存: ...

  7. (转)为Spring集成的Hibernate配置二级缓存

    http://blog.csdn.net/yerenyuan_pku/article/details/52896195 前面我们已经集成了Spring4.2.5+Hibernate4.3.11+Str ...

  8. Hibernate之二级缓存

                                                            Hibernate之二级缓存 一.简介 Gaving King曾经对别人说,hibern ...

  9. hibernate的二级缓存

    缓存(Cache): 计算机领域非常通用的概念.它介于应用程序和永久性数据存储源(如硬盘上的文件或者数据库)之间,其作用是降低应用程序直接读写永久性数据存储源的频率,从而提高应用的运行性能.缓存中的数 ...

随机推荐

  1. easyUI resizable组件使用

    easyUI resizable组件使用: <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  2. android: activity之间切换的抽屉效果

    之前一直用的是向左平移和向右平移的切换动画,看到别的APP那个抽屉效果,自己也弄了一个!感谢给我提供帮助的大神们! 将退出动画的参数设置为0时,进入动画则设置为向左平移,就实现了抽屉效果! 进入的动画 ...

  3. KB奇遇记(4):困难重重的选型

    在以往的工作经历中,虽然也会出现公司的一些规章制度,但我鲜少与其打交道,也极少听说.但是来KB这里,突然发现公司居然并没有给我配备电脑!!原因是制度上并没有写IT人员入职需要配备电脑,尔后通过特批流程 ...

  4. 使用python制作ArcGIS插件(2)代码编写

    使用python制作ArcGIS插件(2)代码编写 by 李远祥 上一章节已经介绍了如何去搭建AddIn的界面,接下来要实现具体的功能,则到了具体的编程环节.由于使用的是python语言进行编程,则开 ...

  5. 成小胖学习ActiveMQ·基础篇

    过了个春节,回到公司的成小胖变成了成大胖.但是你们千万别以为他那个大肚子里面装的都是肥肉,里面的墨水也多了不少嘞,毕竟成小胖利用春节的半个月时间专心学习并研究了 ActiveMQ,嘿嘿……这不,为了检 ...

  6. 构建自动化前端样式回归测试——BackstopJS篇

    在使用scss和less开发的时候,遇到过一件很有趣的事,因为网站需要支持响应式,就开了一个响应式样式框架,简单的几百行scss代码,居然生成了近100KB的css代码,因此决定重构这个样式库.而重构 ...

  7. 配置IIS Express以便通过IP地址访问调试的网站

    问题背景 最近使用C#编写了一个WebService,希望通过Java进行调用.使用Visual Studio 2013调试WebService时,可以在浏览器中通过localhost地址访问WSDL ...

  8. BZOJ 4085:[Sdoi2015]quality(round 2 音质检测)(数据结构)

    居然在考场上把这道题打出来了觉得自己也是有点吊啊(虽然后面就没时间做其他题了囧而且还被卡常数了...) 题解自己写了一份TEX的就直接放上来吧.... 好啦,在谈点什么别的 什么?你在bz上TLE了? ...

  9. JavaScript 毒瘤和糟粕(需要注意的地方)

    简介 我想这是在我总结JavaScript系列中最为需要注意的,最为重要的内容.你必须要去了解这些问题特性,才能准备好应对措施,这真的很重要. 毒瘤 全局变量 全局变量的存在的确是带来了方便,但是我觉 ...

  10. EFcore与动态模型(二)

    上篇文章中介绍了如何使用ef进行动态类型的管理,比如我们定义了ShopDbContext并且注册了动态模型信息,下面的代码实现了动态信息的增加: Type modelType = IRuntimeMo ...