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 包

猜你喜欢

转载自blog.csdn.net/hujiujun/article/details/81701007