java Ehcache緩存過期配置

1.MyCacheEventListener類

package com.seryo.cache;

import com.seryo.util.spring.SpringContextUtil;

import net.sf.ehcache.CacheException;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.event.CacheEventListener;
/**
 * EhcacheCache 緩存 實現配置接口
 * @Package com.seryo.cache
 * @ClassName: MyCacheEventListener
 */
public  class MyCacheEventListener implements CacheEventListener {
	
	private static MyCacheProvider provider;
	
    /**
	 * 因爲啓動初始化的時候spring 拿不到 Service      
	 * @param: @return      
	 * @return: MyCacheProvider      
	 * @throws
	 */
	public static MyCacheProvider getProvider(){
		if(provider == null) {
			provider = SpringContextUtil.getBean(MyCacheProvider.class);
		}
		return provider;
	}
	
	public static CacheEventListener INSTANCE = new MyCacheEventListener();
		/**
		 *  方法會在往Cache中移除單個元素時被調用,即在調用Cache的remove方法之後被調用。
		 */
	   @Override
	   public void notifyElementRemoved(Ehcache cache, Element element)  throws CacheException {
	 
	   }
	 
	   /**
		 *  方法會在往Cache中添加元素時被調用。調用Cache的put方法添加元素時會被阻塞,直到對應的notifyElementPut方法返回之後。
		 */
	   @Override
	   public void notifyElementPut(Ehcache cache, Element element) throws CacheException {
	   }
	 
	   /**
		 *  方法,當往Cache中put一個已經存在的元素時就會觸發CacheEventListener的notifyElementUpdated方法,
		 *  此時put操作也會處於阻塞狀態,直到notifyElementUpdated方法執行完畢。
		 */
	   @Override
	   public void notifyElementUpdated(Ehcache cache, Element element) throws CacheException {
	   }
	 
	   /**
		 *  方法,當Ehcache檢測到Cache中有元素已經過期的時候將調用notifyElementExpired方法。
		 */
	   @Override
	   public void notifyElementExpired(Ehcache cache, Element element) {
            //過期後邏輯處理
		   getProvider().orderFormService.cacheEventUpOrderStatus(element);
	   }
	   /**
		 *  方法將會在元素被驅除的時候調用。
		 */
	   @Override
	   public void notifyElementEvicted(Ehcache cache, Element element) {
	      System.out.println("evicted");
	   }
	 
	   /**
		 *  方法將在調用Cache的removeAll方法之後被調用。
		 */
	   @Override
	   public void notifyRemoveAll(Ehcache cache) {
	      System.out.println("removeAll");
	   }
	 
	   /**
		 *  方法用於釋放資源。
		 */
	   @Override
	   public void dispose() {
	 
	   }
	  
	   public Object clone() throws CloneNotSupportedException {
	      throw new CloneNotSupportedException();
	   }
	   
}

2.MyCacheEventListenerFactory 

package com.seryo.cache;

import java.util.Properties;

import net.sf.ehcache.event.CacheEventListener;
import net.sf.ehcache.event.CacheEventListenerFactory;

/**
 * Event監聽工廠
 * @Package com.seryo.cache
 * @ClassName: MyCacheEventListenerFactory 
 */
public class MyCacheEventListenerFactory extends CacheEventListenerFactory {  
    
    @Override  
    public CacheEventListener createCacheEventListener(Properties properties) {  
    	return MyCacheEventListener.INSTANCE; 
    }  
    
 }  

3.MyCacheProvider

package com.seryo.cache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.seryo.service.bus.OrderFormService;

/**
 * 用來獲得訂單Service
 * @Package com.seryo.cache
 * @ClassName: MyCacheProvider 
 */
@Component
public class MyCacheProvider {
	
	@Autowired
	protected OrderFormService orderFormService;
}

ehcache-core-2.4.3.jar 包

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章