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


    http://blog.csdn.net/yerenyuan_pku/article/details/52896195

    前面我们已经集成了Spring4.2.5+Hibernate4.3.11+Struts1.3.8了,接下来我们就要为Spring4.2.5集成的Hibernate4.3.11配置二级缓存了,因为在企业开发中,会大量使用到Hibernate的二级缓存。 
    由于要在SSH项目中使用到Hibernate4.3.11的二级缓存,所以我们需要向项目中导入Hibernate4.3.11中与二级缓存相关的jar文件,所以要导入如下jar文件: 
     
    注意:以上三个jar文件都要导入到项目中,否则会出现各种异常,比如类找不到等等。 
    这样,总共需要向SSH项目中导入的jar文件有50个: 
     
    接下来我们就来配置Hibernate4.3.11的二级缓存。 
    首先我们要修改Spring配置文件中有关Hibernate的配置信息,修改后的内容为:

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" /> <!-- 数据源  -->
        <property name="mappingResources">
            <list>
                <value>cn/itcast/bean/Person.hbm.xml</value> <!-- Hibernate的实体bean的映射文件(可有多个) -->
            </list>
        </property>
        <!-- hibernateProperties是用来配置Hibernate的属性信息 -->
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
                hibernate.hbm2ddl.auto=update
                hibernate.show_sql=false
                hibernate.format_sql=false
                hibernate.cache.use_second_level_cache=true
                hibernate.cache.use_query_cache=false
                hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider
                hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
            </value>
        </property>
    </bean>
    • 1
    • hibernate.cache.use_second_level_cache=true:使用Hibernate4.3.11的二级缓存。
    • hibernate.cache.use_query_cache=false:不使用Hibernate4.3.11的查询缓存,因为一般而言,查询缓存的命中率并不是很高,所以我们没有必要为每个用户的查询缓存他的数据。
    • hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider:指定使用缓存产品的驱动类,在该应用中我们使用了EhCacheProvider这个缓存产品。
    • hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory:该属性是什么意思,我就不知道了,但是得配置,不然就会报错

    再接着,我们就要在类路径下新建Ehcache默认的配置文件ehcache.xml,一开始将其内容写为:

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache>
        <diskStore path="D:cache" />
        <defaultCache maxElementsInMemory="1000" eternal="false" overflowToDisk="true" 
            timeToIdleSeconds="120" 
            timeToLiveSeconds="180"
            diskPersistent="false" 
            diskExpiryThreadIntervalSeconds="60" />
    </ehcache>
    • 1
    • diskStore:指定缓存的对象存放在硬盘上的哪个路径底下。
    • defaultCache:定义缓存的默认行为,即为缺省的缓存策略。 
      • maxElementsInMemory:在缓存中默认存放的元素(对象)个数,即内存中最大允许存在的对象数量。
      • eternal:设置缓存中的对象是否永远不过期,true代表永远不过期,一直存在,false代表要过期了。
      • overflowToDisk:当缓存对象达到内存中最大允许存在的数量时,是否把溢出的对象存放到硬盘上。
      • timeToIdleSeconds:指定缓存对象空闲多长时间就过期,过期的对象会被清除掉。
      • timeToLiveSeconds:指定缓存对象总的存活时间,超过这个值就会被清除掉。
      • diskPersistent:当你的缓存应用关闭的时候,是否需要把缓存的对象持久化到硬盘上,即当JVM结束时是否持久化对象。
      • diskExpiryThreadIntervalSeconds:指定专门用于清除过期对象的监听线程的轮询时间,也就是说后面有一个线程,它会不断扫描,扫描是否有对象过期,有对象过期,就会将其清除掉。

    然后在需要缓存的实体bean配置文件中加入缓存配置项,如Person这个实体bean需要使用缓存,可配置一个缓存:

    <cache usage="read-write" region="cn.itcast.bean.Person" />
    • usage:缓存策略,这里可以采用读和写。两个并发的事务可对这个对象同时进行读的操作,但是一个事务对它进行写的话,另一个事务是不能够对它进行读的。
    • region:指定缓存的区域名,在区域名里面来存放缓存的对象。区域名可定义为实体类的全称。

    这样,Person这个实体bean的映射文件就该为:

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="cn.itcast.bean">
        <class name="Person" table="person">
            <cache usage="read-write" region="cn.itcast.bean.Person" />
            <id name="id" type="int" column="id">
                <generator class="native"/>
            </id>
            <property name="name" type="string" length="20" not-null="true" column="name" />
        </class>
    </hibernate-mapping>
    • 1

    当然我们也可以为cn.itcast.bean.Person缓存域来定义一些特殊的缓存设置,如果不定义的话,那么它默认使用的是这种缓存策略:

    <defaultCache maxElementsInMemory="1000" eternal="false" overflowToDisk="true" 
            timeToIdleSeconds="120" 
            timeToLiveSeconds="180"
            diskPersistent="false" 
            diskExpiryThreadIntervalSeconds="60" />
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5

    如果cn.itcast.bean.Person缓存域需要一些特别的缓存设置,我们可以为它定义:

    <cache name="cn.itcast.bean.Person" maxElementsInMemory="100" eternal="false"
           overflowToDisk="true" 
           timeToIdleSeconds="300" 
           timeToLiveSeconds="600" 
           diskPersistent="false"/>
    • 1

    这样,Ehcache默认的配置文件ehcache.xml的内容就应该是:

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache>
        <diskStore path="D:cache" />
        <defaultCache maxElementsInMemory="1000" eternal="false" overflowToDisk="true" 
            timeToIdleSeconds="120" 
            timeToLiveSeconds="180"
            diskPersistent="false" 
            diskExpiryThreadIntervalSeconds="60" />
        <cache name="cn.itcast.bean.Person" maxElementsInMemory="100" eternal="false"
               overflowToDisk="true" 
               timeToIdleSeconds="300" 
               timeToLiveSeconds="600" 
               diskPersistent="false"/>
    </ehcache>
    • 1

    那怎么检验Person实体bean应用上了缓存呢?我们可以按照这个思路来写代码:如果一旦缓存里面存在了某个id的Person对象之后,第二次再去请求这个相同id的Person对象时,它是不会从数据库里面获取数据的,而是从内存里面获取到缓存对象。依照这个思路我们需要修改PersonServiceTest类中testGetPerson()方法的代码为:

    @Test
    public void testGetPerson() {
        Person person = personService.getPerson(2);
        System.out.println(person.getName());
        try {
            System.out.println("请关闭数据库,即停止MySQL服务");
            Thread.sleep(1000*60);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("第二次开始获取");
        person = personService.getPerson(2);
        System.out.println(person.getName());
    }

    当我们首次测试testGetPerson(),然后迅速停止掉MySQL服务,过约莫1分钟,可发现Eclipse控制台又会打印出id为2的Person对象的name,从而知道第二次是从内存里面获取到缓存对象的。 
    如须查看源码,可点击为Spring集成的Hibernate配置二级缓存进行下载。

  • 相关阅读:
    Git 安装配置,key导入
    Android集成支付宝的坑
    RxJava 的使用入门
    RecyclerView 介绍 02 – 重要概念
    AndroidTips:selector的disable状态为什么无效?
    AndroidTips:解决Dialog全屏显示以及Dialog显示自动弹出输入法
    RecyclerView 介绍 01
    python模块-----sqlAlchemy
    python模块-----pyinstaller
    python模块------socket
  • 原文地址:https://www.cnblogs.com/telwanggs/p/6913586.html
Copyright © 2020-2023  润新知