spring緩存 ehcache

openkk 12年前發布 | 3K 次閱讀 LedisDB

  ehcache 與spring集成實現簡單的數據集緩存,事例是在ssh集成上實現,不對ssh做闡述。

 

1 : lib追加jar包

       ehcache-1.2.4.jar

 

2 : applicationContext-service.xml

      <bean id="GatewayIPManager"    class="cn.com.superv.netmessage.oam.web.manager.GatewayIPManager">

    <property name="transactionManager">

        <ref bean="transactionManager" />

    </property>

    <property name="interProtocolDAO">

        <ref bean="InterProtocolDAO" />

    </property>

    <!-- 緩存對象-->

    <property name="gatewayIPCache" ref="GatewayIPManagerBean"/>

</bean></pre><p></p>

3 : applicationContext-cache.xml  緩存事例配置文件

     <?xml version="1.0"?>

 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

    "http://www.springframework.org/dtd/spring-beans.dtd">



  <beans>

<!-- 緩存管理 -->

  <bean id="cacheManager"

     class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

     <property name="configLocation"

        value="WEB-INF/ehcache.xml">

     </property>

   </bean>



   <bean id="GatewayIPManagerBean"       class="org.springframework.cache.ehcache.EhCacheFactoryBean">

      <property name="cacheManager" ref="cacheManager"></property>

      <property name="cacheName" value="GatewayIPManager"></property>

    </bean>

  </beans></pre><p></p>

 

4  : WEB-INF中添加ehcache.xml 緩沖對象屬性配置 

        <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="ehcache.xsd">
          <diskStore path="java.io.tmpdir"/>
          <!-- 
          maxElementsInMemory:  最大緩存100個對象 
                    eternal:不是永久有效
             overflowToDisk:緩存滿了不寫的磁盤
          timeToIdleSeconds:180秒后沒有讀就失效
          timeToLiveSeconds:300秒后失效
          memoryStoreEvictionPolicy:當緩存數達到最大,將移除使用最少的緩存對象
          -->
         <!-- 默認緩存設置 -->
        <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="180"
            timeToLiveSeconds="300"
            overflowToDisk="true"
            memoryStoreEvictionPolicy="LRU"
            />
         <!-- 緩存設置 -->
        <cache name="GatewayIPManager"
           maxElementsInMemory="100"
           eternal="false"
           overflowToDisk="false"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="LFU"
           />
        </ehcache>



5  : GatewayIPManager class編寫

        private Cache gatewayIPCache ; 
public void setGatewayIPCache(Cache gatewayIPCache) {
this.gatewayIPCache = gatewayIPCache;
}
private InterProtocolDAO interProtocolDAO ;

public void setInterProtocolDAO(InterProtocolDAO interProtocolDAO) { this.interProtocolDAO = interProtocolDAO; } public boolean isExist(String wapip){

    if (StringUtils.isEmpty(wapip)){
     throw new NullPointerException("param 'wapid' can't be null ! ");
 }
    List<String> wapIPList = null ;
   //如果緩存對象不存在,去數據庫中獲取
       Element element = gatewayIPCache.get(wapip);
       if (element == null) {
           wapIPList = interProtocolDAO.getInterProtocolsByType(InterProtocol.WAP_IP);
           element = new Element(wapip, wapIPList);
           gatewayIPCache.put(element);
           if (log.isInfoEnabled()) {
               log.info("----------數據列表加載到緩存中-------------");
           }
        }
        wapIPList = (List<String>) element.getValue();
    return wapIPList.contains(wapip);

     }</pre>

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!