zoukankan      html  css  js  c++  java
  • 缓存二级缓存Spring环境下Hibernate二级缓存的应用

    发一下牢骚和主题无关:

        在数据不经常变化的地方,为了可以提高网站系统的执行效率,使用缓存是我们经常想到的处理方式。Hibernate的缓存分为三类,我们在平时应用开发中,最容易疏忽的就是它的一级缓存,因为它的生命周期与session分歧。二级缓存的应用是最广泛的,也是卓有成效的。那么怎么配置它呢?重要分为以下几步:

        《1》搭建Spring+Hibernate环境

        《2》加载Hibernate-ehcache相应的jar包

        《3》在applicationContext.xml中添加缓存的相关配置:

                 <bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" ></property>
    <property name="mappingDirectoryLocations">
    <list>
    <value>classpath:com/entity/test</value>
    </list>
    </property>
    <property name="hibernateProperties">
       <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
    <prop key="hibernate.show_sql">false</prop> 
    <!-- 开启hibernate的查询缓存 -->
           <prop key="hibernate.cache.use_query_cache">true</prop>
           <!-- 开启hibernate二级缓存-->
           <prop key="hibernate.cache.use_second_level_cache">true</prop>
           <!-- hibernate缓存管理类-->
           <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                            <!-- hibernate缓存配置文件-->
           <prop key="hibernate.cache.configurationResourceName">ehcache.xml</prop>
      </props>
    </property> 
    </bean>

        每日一道理
    闷热的天,蝉儿耐不住寂寞地不停在鸣叫,我孤单一人,寂静的身旁没有一个知音,想疯狂地听摇滚乐,听歇斯底里的歌声,那只为逃避无人的世界里那浓烈的孤单气息。一个人是清冷,两个人便是精彩,于是,莫名的冲动让我格外想念旧日的好友,怀念过去的日子,尽管不够现实的遐想追回不了曾经一切,但却希望思绪可以飞扬于闭上双目后的世界中,印有微笑,印有舞动的身姿,翩翩起舞……

        《4》在src目录下创建ehcache.xml缓存文件,内容大致如下:

        

           <?xml version="1.0" encoding="UTF-8"?>
           <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
              <diskStore path="java.io.tmpdir" />
              <defaultCache eternal="false" 
                                     maxElementsInMemory="1000"
                                     overflowToDisk="false" 
        diskPersistent="false" 
        timeToIdleSeconds="0"
       timeToLiveSeconds="600" 
        memoryStoreEvictionPolicy="LRU" />

      <cache name="departCache" 
           eternal="false"
            maxElementsInMemory="100"
            overflowToDisk="false"
            diskPersistent="false" 
           timeToIdleSeconds="0"
            timeToLiveSeconds="300"
            memoryStoreEvictionPolicy="LRU" />

        </ehcache>

        其中详细的含意可以参考网上的定义,这里不再标注。

        《5》在dao中引入二级缓存:
                 
    HibernateTemplate template = this.getHibernateTemplate();     
         template.setCacheQueries(true);//开启hibernate二级缓存     
         List result =  template.find(sql);   
         template.setCacheQueries(false);     

        
    总结:初次查询会连接数据库,其后会从缓存中返回结果,而不会继承从数据库中读取。

        

        

        

    文章结束给大家分享下程序员的一些笑话语录: 手机终究会变成PC,所以ip会比wm更加畅销,但是有一天手机强大到一定程度了就会发现只有wm的支持才能完美享受。就好比树和草,草长得再高也是草,时间到了条件成熟了树就会窜天高了。www.ishuo.cn

  • 相关阅读:
    Go中的interface学习
    Go中的命名规范
    Go中的结构体
    Go中的文件读写
    Go包管理工具dep
    SpringBoot中读取配置文件的几种方式
    Go标准库--net/http学习
    centos7通过yum安装docker
    scala之构造器详解
    解决!!-- krb5-libs.x86_64被卸载,yum不能使用,ssh不能连接
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3084607.html
Copyright © 2011-2022 走看看