EHCache的简单使用

一、创建ehcache配制文件,放到classes目录下

<?xml version="1.0" encoding="UTF-8"?>
<!-- <ehcache xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”ehcache.xsd”
updateCheck="true" monitoring="autodetect" dynamicConfig="true"> -->
<ehcache xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
 <!-- 磁盘存储路径 -->   
    <diskStore path="c:\\myapp\\cache"/>   
    <defaultCache  
        maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="true"  
        diskSpoolBufferSizeMB="30"  
        maxElementsOnDisk="10000000"  
        diskPersistent="false"  
        diskExpiryThreadIntervalSeconds="120"  
        memoryStoreEvictionPolicy="LRU"  
        />

</ehcache>

 

二、修改hibernate.cfg.xml配制文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</property>
    <property name="net.sf.ehcache.configurationResourceName">ehcache.xml</property>
    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.use_query_cache">true</property>

    <property name="hibernate.generate_statistics">true</property>
    <property name="show_sql">true</property>
    <!-- <property name="hbm2ddl.auto">update</property>    
    <property name="namingStrategy">net.sf.hibernate.cfg.ImprovedNamingStrategy</property> -->
    
    <!-- pojo 配制
    <mapping class="cn.eaglelink.cache.pojo.UserInfo"/>-->
    <mapping resource="cn/eaglelink/pojo/UserInfo.hbm.xml"/>       
    
  </session-factory>
</hibernate-configuration>

三、spring配制文件如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="
http://www.springframework.org/schema/beans"
 xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans
 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">    
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    <property name="lobHandler" ref="lobHandler"></property>
  </bean>  
    
  <bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>
   
  <bean id="myDaoTxProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="myTxManager"/>
    <property name="transactionAttributes">
      <props>
        <prop key="save*">PROPAGATION_REQUIRED</prop>
        <prop key="update*">PROPAGATION_REQUIRED</prop>
        <prop key="delete*">PROPAGATION_REQUIRED</prop>
        <prop key="*">PROPAGATION_REQUIRED</prop>
      </props>
    </property>
  </bean>
  
  <bean id="myServiceTxProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="myTxManager"/>
    <property name="transactionAttributes">
      <props>
        <prop key="save*">PROPAGATION_REQUIRED</prop>
        <prop key="update*">PROPAGATION_REQUIRED</prop>
        <prop key="delete*">PROPAGATION_REQUIRED</prop>
        <prop key="find*">PROPAGATION_REQUIRED</prop>
        <prop key="*">PROPAGATION_REQUIRED</prop>
      </props>
    </property>
  </bean>
    
  <bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>
  
  <!-- mysql data source -->   
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
   <property name="driverClassName">
   <value>com.mysql.jdbc.Driver</value>
   </property>
   <property name="url">
   <!-- <value>jdbc:mysql://sapp1i.uyunke.com:8066/eaglelinkcore?useUnicode=true&amp;characterEncoding=utf-8</value>  -->
   <value>jdbc:mysql://localhost:3306/eaglelinkcas?useUnicode=true&amp;characterEncoding=utf-8</value>
   </property>
   <property name="username">
   <value>root</value>
   </property>
   <property name="password">
   <value>root</value>
   </property>
  </bean>
  
  <!-- 引用ehCache的配置  -->
     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> 
         <property name="configLocation">  
             <value>classpath:ehcache_ls.xml</value>  
         </property>
     </bean>

  <!-- *********************** -->
  <!-- ********  DAO  ******** -->
  <!-- *********************** -->  
  <!-- 应用dao管理 -->
  <bean id="userDaoTarget" class="cn.eaglelink.dao.impl.UserDaoImpl">
    <property name="sessionFactory">
      <ref bean="sessionFactory"/>
    </property>
  </bean>
  <bean id="userDao" parent="myDaoTxProxy">
    <property name="target">
      <ref bean="userDaoTarget"/>
    </property>
  </bean>  
  
  <!-- *********************** -->
  <!-- ******  Service  ****** -->
  <!-- *********************** -->  
  <!-- 应用service管理
  <bean id="appServiceTarget" class="cn.eaglelink.cas.service.impl.AppServiceImpl">
     <property name="appDao">
       <ref bean="appDao"/>
     </property>    
  </bean>
  <bean id="appService" parent="myServiceTxProxy">
     <property name="target">
        <ref bean="appServiceTarget"/>
     </property>
  </bean> -->
  
  <!-- 配置 BeanFactoryAwarebean -->
  <bean id="nativeBean" class="cn.eaglelink.util.NativeBeanFactory" />  
      
</beans>

 

四、在需要增加缓存的DAO中修改:

 

public class UserDaoImpl extends BaseDaoImpl<UserInfo> implements IUserDao{

 @SuppressWarnings("unchecked")
 @Override
 public List<UserInfo> queryAllUser() {
  try{
   Query query=this.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery("from UserInfo");
   
query.setCacheable(true);
   List<UserInfo> list=query.list();
   if(list.size()>0){
    return list;
   }   
  }catch(Exception e){
   e.printStackTrace();
  }
  return null;
 } 

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值