zoukankan      html  css  js  c++  java
  • 配置Hibernate的二级缓存

    1.在applicationContex.xml文件里面添加二级缓存配置:

      <!-- 配置hibernate的sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    	<property name="dataSource"><ref bean="dataSource" /></property>
    	 <property name="mappingResources">
    		    <list>
    		      <value>com/persia/model/Person.hbm.xml</value>
    		    </list>
    		 </property>
    	 <property name="hibernateProperties">
    		    <value>
    		        hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
    		        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=org.hibernate.cache.EhCacheProvider //使用Ehcache
    		      </value>
    	     </property>
    </bean>

    2.添加ehcache.xml的缓存配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- 
         defaultCache节点为缺省的缓存策略
         maxElementsInMemory 内存中最大允许存在的对象数量
         eternal 设置缓存中的对象是否永远不过期
         overflowToDisk 把溢出的对象存放到硬盘上
         timeToIdleSeconds 指定缓存对象空闲多长时间就过期,过期的对象会被清除掉
         timeToLiveSeconds 指定缓存对象总的存活时间
         diskPersistent 当jvm结束是是否持久化对象
         diskExpiryThreadIntervalSeconds 指定专门用于清除过期对象的监听线程的轮询时间
     -->
    <ehcache>
        <diskStore path="D:cache"/>
        <defaultCache  maxElementsInMemory="1000" eternal="false" overflowToDisk="true"
            timeToIdleSeconds="120"
            timeToLiveSeconds="180"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="60"/>
    <cache name="com.persia.model.Person" maxElementsInMemory="100" eternal="false"
        overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" diskPersistent="false"/>
    </ehcache>
    

    3.在实体bean的映射文件添加缓存设置:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.persia.model">
        <class name="Person" table="person">
        	<cache usage="read-write" region="com.persia.model.Person"/>
            <id name="id">
                <generator class="native"/>
            </id>
            <property name="name" length="30" not-null="true"/>
        </class>
    </hibernate-mapping>

    4.测试是否使用缓存

      第一次获取从数据库获取,然后缓存,关闭数据库,进行第二次获取,看是否能获取得到。

    @Test
    	public void testGetPerson() {
    		System.out.println(ps.getPerson(7).getName());
    		try {
    		
    			System.out.println("请在15秒之内关闭数据库");
    			Thread.sleep(15*1000);
    			
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		System.out.println("从缓存获取:"+ps.getPerson(7).getName());
    	}

    结果:

    传智播客4
    请在15秒之内关闭数据库
    从缓存获取:传智播客4

    查看磁盘的缓存文件:

    image

    分类: Spring
  • 相关阅读:
    基于讯飞语音API应用开发之——离线词典构建
    在Linux下安装redis
    java学习路线
    怎么做压力测试
    性能测试概念及参数介绍
    总结1-JMeter压力测试
    在软件测试面试过程中如何进行自我介绍?
    Python模块常用的几种安装方式
    Jmeter(三)关联数组
    Jmeter(二)关联
  • 原文地址:https://www.cnblogs.com/hanxue112253/p/3866162.html
Copyright © 2011-2022 走看看