Ehcache二级缓配置永不过期,缓存失效问题 ---- 原因没有加载ehcache.xml配置文件,加载了默认的ehcache-failsafe.xml配置文件

       问题: 在ehcache.xml配置文件中的参数 配置的空闲时间,过期时间都很长, 或者说配置的永不过期, 但是在测试项目过程中, 程序还是会有重新查数据库, 配置参数,配置文件好像不生效问题;

       解决:  有可能是 加的ehcache.xml配置文件, 项目并没有真正引用进去, 项目而是加载的默认的配置文件; 

       有没有真正加载自己写的配置文件, 最简单直接的验证办法是, 修改ehcache.xml 中的 在硬盘上存储对象的路径:    <diskStore path="java.io.tmpdir" />     改为自己建立的文件夹:   <diskStore path="D:\\ehcachedata2" />      然后启动项目, 文件夹会有类的data生成, 则证明加载了该配置文件, 该配置生效;

例: com.test.unit.Unit.data

com.test.users.Users.data

怎样加载 自己的配置文件ehcache.xml, 要根据自己的项目搭建, 加载进去;

 

 

引用的ehcache.jar包中有一个ehcache-failsafe.xml 配置文件,如果没有配置ehcache.xml文件,ehcache-failsafe.xml将作为默认的ehcache配置。

默认的二级缓存的配置文件位置:

 

 

 

二级缓存添加的实例:  项目  spring+ Struts+hibernate 的配置文件方式:

 

1 applicationContext.xml 配置开启二级缓存:

		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					${hibernate.dialect}
				</prop>
				<prop key="hibernate.show_sql">false</prop>
				
				
				<prop key="hibernate.cache.use_second_level_cache">true</prop> 	<!-- 开启二级缓存 ehcache -->
				<prop key="hibernate.cache.use_query_cache">true</prop> 	<!-- 开启查询的二级缓存  -->
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
				<prop key="hibernate.generate_statistics">true</prop><!-- 开启二级缓存的统计功能	 -->
				<prop key="hibernate.cache.use_structured_entries">true</prop><!-- 设置使用结构化方式来维护缓存项 -->
				<prop key="hibernate.current_session_context_class">thread</prop><!-- 当前session的上下文 保证在同一线程中获取到的是同一个session -->
				
				<!-- <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>
				 --><!-- 引入配置文件, 一般放对位置, 项目自动引入,不用手动引入 -->
			
			</props>
		</property>
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>

 

2 pojo对应的xml映射文件 加入 <cache usage="read-write"/>  ;   read-write是缓存策略之一;

<hibernate-mapping auto-import="false">
    <class name="com.test.doc" table="DOC"
     dynamic-insert="true" dynamic-update="true" >
     
    	<cache usage="read-write"/>
    	
        <id name="docId" type="java.lang.Integer" unsaved-value="0">
            <column name="DocId" />
           <generator class="sequence">
              <param name="sequence">S_DOC</param> 
            </generator>
        </id>
 
        <property name="docTitle" type="java.lang.String">
            <column name="DocTitle"/>
        </property>
          
    </class>
</hibernate-mapping>

3:dao下的添加:  query.setCacheable(true); / getHibernateTemplate().setCacheQueries(true);

    public List find(String where){
    	getHibernateTemplate().setCacheQueries(true);
    	return getHibernateTemplate().find(where);
    }


----------


    public List findHql(String where){
        List items=new ArrayList();     
        Session session = getSession();        
        Query query = session.createQuery(where); 
        query.setCacheable(true);
        
        List list= query.list();
        releaseSession(session);
        return list;
            
    }

 

4: ehcache.xml:

 

        <?xml version="1.0" encoding="UTF-8"?>
        <ehcache>    
           <diskStore path="D:\\ehcachedata" />
          <defaultCache maxElementsInMemory="5"<!--缓存可以存储的总记录量-->
            eternal="false"<!--缓存是否永远不销毁-->
            overflowToDisk="true"<!--当缓存中的数据达到最大值时,是否把缓存数据写入磁盘-->
            timeToIdleSeconds="15"<!--当缓存闲置时间超过该值,则缓存自动销毁-->
                timeToLiveSeconds="120"<!--缓存创建之后,到达该缓存自动销毁-->
          />
        </ehcache>

 

具体详解: 参考引用 http://www.blogjava.net/i369/articles/219407.html

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值