zoukankan      html  css  js  c++  java
  • SSH2中memcached作为hibernate二级缓存

    一、安装memcached服务端

    1. 下载memcached的windows稳定版,解压放某个盘下面,比如在c:memcached
    2. 在CMD下输入 "c:memcachedmemcached.exe -d install" 安装.
    3. 再输入:"c:memcachedmemcached.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,第一次之后的查询在缓存生命周期内都不用到数据库去取数据了。

    我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他 们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其 实我是一个程序员
  • 相关阅读:
    POJ 3255 Roadblocks
    KMP算法的前缀next数组最通俗的解释
    HDU 1829 A Bug's Life
    HDU 1879 继续畅通工程
    课程设计:学生管理系统(c++)
    HDU 1016 Prime Ring Problem
    HDU 4310 Hero
    素数筛选法<单向链表实现>
    【未完】训练赛20190304:KMP+树状数组+线段树+优先队列
    畅通工程:并查集入门裸题
  • 原文地址:https://www.cnblogs.com/kms1989/p/5542443.html
Copyright © 2011-2022 走看看