一、安装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二级缓存的更多相关文章

  1. Hibernate 二级缓存 总结整理(转)

    和<Hibernate 关系映射 收集.总结整理> 一样,本篇文章也是我很早之前收集.总结整理的,在此也发上来 希望对大家有用.因为是很早之前写的,不当之处请指正. 1.缓存:缓存是什么, ...

  2. Hibernate ——二级缓存

    一.Hibernate 二级缓存 1.Hibernate 二级缓存是 SessionFactory 级别的缓存. 2.二级缓存分为两类: (1)Hibernate内置二级缓存 (2)外置缓存,可配置的 ...

  3. 配置Hibernate二级缓存步骤

    配置Hibernate二级缓存步骤: 加入二级缓存的jar包及配置文件 jar包位置:hibernate-release-4.1.8.Final\lib\optional\ehcache下所有jar包 ...

  4. Hibernate(十六):Hibernate二级缓存

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

  5. Hibernate二级缓存简述及基于Spring4,Hibernate5,Ehcache3的二级缓存配置

    Hibernate L2缓存 缓存的分类 L2缓存工作原理 放入二级缓存的数据 Ehcache 依赖 ehcache.xml 常用的memoryStoreEvictionPolicy(缓存算法) eh ...

  6. hibernate二级缓存demo2

    @Test public void hello3(){ Session session=sessionFactory.openSession(); List list = session.create ...

  7. hibernate二级缓存整合

    <?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http:// ...

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

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

  9. spring boot集成ehcache 2.x 用于hibernate二级缓存

    https://www.jianshu.com/p/87b2c309b776 本文将介绍如何在spring boot中集成ehcache作为hibernate的二级缓存.各个框架版本如下 spring ...

随机推荐

  1. 小课堂Week8 例外处理设计的逆袭Part1

    小课堂Week8 例外处理设计的逆袭Part1 今天和大家讲一本书,书名是<例外处理设计的逆袭>. 为什么想讲这本书,是因为,例外处理在程序代码中到处存在,但是这些到底该如何写好,总觉得有 ...

  2. 【转】 Linux Core Dump 介绍

    ===============================================================  Linux core dump的祥细介绍和使用 =========== ...

  3. 在 Windows 8 或 8.1 上安装 .NET Framework 3.5 安装错误:0x800f0906、0x800F081F

    昨天给一天新装Windows 8.1的PC装.NET Framework 3.5 发现联网速度很慢,并且在长久等待过后直接报错了:0x800f0906 经过Bing,发现了解决方案: 如果根据需要安装 ...

  4. 1509 -- Glass Beads POJ

    题意:求一个字符串的最小表示的开始下标 就当模板题写了 把字符串重复一遍,再建后缀自动机,贪心的选最小字典序在上面走len步 因为走出来的一定是子串,长度又是len,所以一定是原来的字符串旋转得到的, ...

  5. [scalability] Find all documents that contain a list of words

    Given a list of millions of documents, how would you find all documents that contain a list of words ...

  6. Educational Codeforces Round 5 A

    Problem A:http://codeforces.com/contest/616/problem/A A. Comparing Two Long Integers 果然还是我太天真了(长整数比较 ...

  7. leetcode5 Implement strstr() 实现strstr函数功能

    Implement strstr() 实现strstr函数功能 whowhoha@outlook.com Question: Implement strstr(). Returns the index ...

  8. [BEC][hujiang] Lesson03 Unit1:Working life ---Grammar & Listening & Vocabulary

    3 Working life p8 Grammar Gerund and infinitive(动名词和不定式) 一般而言:        1 动词后面接动名词还是不定式没有特定规则,主要取决于语言习 ...

  9. SqlServer日志

    Sqlserver 2005日志查看.恢复工具 log exploer4.1 SqlServer2008 日志查看  系统函数 select * from fn_dblog(null,null) 参考 ...

  10. C++中的构造函数,拷贝构造函数和赋值运算

    关于C++中的构造函数,拷贝构造函数和赋值运算,以前看过一篇<高质量C++/C编程指南>的文章中介绍的很清楚,网上能搜索到,如果想详细了解这方面的知识可以参看一下这篇文章. 常见的给对象赋 ...