SSH2中memcached作为hibernate二级缓存
一、安装memcached服务端
1. 下载memcached的windows稳定版,解压放某个盘下面,比如在c:\memcached
2. 在CMD下输入 "c:\memcached\memcached.exe -d install" 安装.
3. 再输入:"c:\memcached\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>
<class name="hibernate.TbUserbasic" table="tb_userbasic" catalog="db_xydate">
<cache usage="read-write"/>
<id name="userId" type="java.lang.Integer">
<column name="UserId" />
<generator class="assigned" />
</id>
<property name="userName" type="java.lang.String">
<column name="UserName" length="20" not-null="true" />
</property>
...

3、查询语句前
加上query.setCacheable(true);//这样才能使查询的时候调用缓存!

Session session=this.getHibernateTemplate().getSessionFactory().openSession();
Query query=session.createQuery(sql);
query.setCacheable(true);//这样才能使查询的时候调用缓存!
query.setFirstResult(start);
query.setMaxResults(size);
List list=query.list();
System.out.println(list.size());
session.close();
return list;

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

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

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

Ok,第一次之后的查询在缓存生命周期内都不用到数据库去取数据了。
SSH2中memcached作为hibernate二级缓存的更多相关文章
- Hibernate 二级缓存 总结整理(转)
和<Hibernate 关系映射 收集.总结整理> 一样,本篇文章也是我很早之前收集.总结整理的,在此也发上来 希望对大家有用.因为是很早之前写的,不当之处请指正. 1.缓存:缓存是什么, ...
- Hibernate ——二级缓存
一.Hibernate 二级缓存 1.Hibernate 二级缓存是 SessionFactory 级别的缓存. 2.二级缓存分为两类: (1)Hibernate内置二级缓存 (2)外置缓存,可配置的 ...
- 配置Hibernate二级缓存步骤
配置Hibernate二级缓存步骤: 加入二级缓存的jar包及配置文件 jar包位置:hibernate-release-4.1.8.Final\lib\optional\ehcache下所有jar包 ...
- Hibernate(十六):Hibernate二级缓存
Hibernate缓存 缓存(Cache):计算机领域非常通用的概念.它介于应用程序和永久性数据存储源(如磁盘上的文件或者数据库)之间,起作用是降低应用程序直接读取永久性数据存储源的频率,从而提高应用 ...
- Hibernate二级缓存简述及基于Spring4,Hibernate5,Ehcache3的二级缓存配置
Hibernate L2缓存 缓存的分类 L2缓存工作原理 放入二级缓存的数据 Ehcache 依赖 ehcache.xml 常用的memoryStoreEvictionPolicy(缓存算法) eh ...
- hibernate二级缓存demo2
@Test public void hello3(){ Session session=sessionFactory.openSession(); List list = session.create ...
- hibernate二级缓存整合
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http:// ...
- ssh整合hibernate 使用spring管理hibernate二级缓存,配置hibernate4.0以上二级缓存
ssh整合hibernate 使用spring管理hibernate二级缓存,配置hibernate4.0以上二级缓存 hibernate : Hibernate是一个持久层框架,经常访问物理数据库 ...
- spring boot集成ehcache 2.x 用于hibernate二级缓存
https://www.jianshu.com/p/87b2c309b776 本文将介绍如何在spring boot中集成ehcache作为hibernate的二级缓存.各个框架版本如下 spring ...
随机推荐
- SqlServer中创建Oracle连接服务器
转自太祖元年的:http://www.cnblogs.com/jirglt/archive/2012/06/10/2544025.html参考:http://down.51cto.com/data/9 ...
- 1046 Shortest Distance (20)
#include<stdio.h> int main() { int n,m,a,b,tem,pre,p; int i,j; ]; while(scanf("%d",& ...
- fedora 禁止nouveau加载
To remove / disable nouveau drivers from kernel initramfs ## Backup old initramfs nouveau image ## m ...
- Datatables中文API——回调函数
fnCookieCallback:还没有使用过 $(document).ready(function () { $('#example').dataTable({ "fnCookieCall ...
- WinForm控件选择器
jQuery和Css的控件选择器用起来非常畅快,相信用过的人都会有这种感觉,而WinForm则是通过Name来实现窗体中控件的选择,在选择单个控件的时候是很方便,但是当选择具有一类特征的控件时,则显得 ...
- C#正则表达式Regex类
C#正则表达式Regex类的使用 C#中为正则表达式的使用提供了非常强大的功能,这就是Regex类.这个包包含于System.Text.RegularExpressions命名空间下面,而这个命名空间 ...
- Delphi XE5 如何设计并使用FireMonkeyStyle(转)
如何设计并使用FireMonkeyStyle FireMonkey使用Style来控制控件的显示方式. 每个控件都有一个StyleLookup属性,FireMonkey就是通过控件的这个属性来在当前窗 ...
- 手机开发类型jquery的框架Zepto(API)
Zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与jquery有着类似的api. 如果你会用jquery,那么你也会用zepto. http://www.html-5.cn/M ...
- linux下执行 ls,cat等一些命令报出 -bash: /bin/cat: Cannot allocate memory 有没解决的方法
环境变量配置出错了cd -- 进入用户目录vim .bash_profile删除以前PATH这一行,把下面的粘帖进去PATH=$PATH:$HOME/bin:/root:/root/snapshot/ ...
- mysql学习链接
1 传智播客PHP培训.刘道成.PHP视频教程.mysql http://down.51cto.com/zt/887