最近公司公司项目在框架上用到了Ehcache缓存,关于更多Ehcache详细的内容,请参考一下两个系列的博客,讲的都很好。

Ehcache详细解读

Ehcache系列博客

 有了上面的两个系列的博客,更多有关于Ehcache细节的地方,就不在这篇博客中展示了,主要是跟大家分享一下,底层框架中封装关于Ehcache的工具类


  CacheContainer类

  缓存容器类,用来创建Cache对象,并且用来实现CRUD对象的一系列操作

/**     
 * @FileName: CacheContainer.java   
 * @Package:com.tgb.util.Cache   
 * @Description: TODO  
 * @author: LUCKY    
 * @date:2015年11月28日 下午3:27:52   
 * @version V1.0     
 */
package com.tgb.util.Cache;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * @ClassName: CacheContainer
 * @Description: 缓存容器的类
 * @author: LUCKY
 * @date:2015年11月28日 下午3:27:52
 */
public class CacheContainer {

	public static final String DEFAULT_STRATEGY_NAME = "tgbTest";

	private CacheManager ehManager;
	private static String default_strategy = String
			.valueOf(DEFAULT_STRATEGY_NAME);

	private static class SingletonHolder {
		static CacheContainer instance = new CacheContainer();
	}

	public static CacheContainer getInstance() {
		return SingletonHolder.instance;
	}

	private CacheContainer() {
		ehManager = new CacheManager(Thread.currentThread()
				.getContextClassLoader().getResourceAsStream("ehcache.xml"));

	}

	public static boolean containskey(String key) {
		return containskey(default_strategy, key);
	}

	public static boolean containskey(String cacheStrategy, String key) {
		return getInstance().ehManager.getCache(cacheStrategy)
				.isKeyInCache(key);
	}

	public static void removeCache(String cacheName) {
		getInstance().ehManager.removeCache(cacheName);
	}

	public static void removeCacheObject(String cacheName, String key) {
		getInstance().ehManager.getCache(cacheName).remove(key);
	}

	public static void removeCacheObjectAll(String cacheName) {
		getInstance().ehManager.getCache(cacheName).removeAll();
	}

	public static void addCache(String key, Object value) {

	}

	public static void addCache(String cacheName, String key, Object value) {
		Cache cache = getInstance().ehManager.getCache(cacheName);
		if (cache != null) {
			Element el = new Element(key, value);
			cache.put(el);
		}
	}

	
	public static Object getCacheObject(String cacheName,String key){
		Element element=getInstance().ehManager.getCache(cacheName).get(key);
		if(element==null){
			return null;
		}
		return element.getObjectValue();
	}
}

  PlatformCacheManagerEventListenerFactory类

  该类是一个工厂类,用来创建具体的CacheManagerEventListener的实现

/**     
 * @FileName: PlatformCacheManagerEventListenerFactory.java   
 * @Package:com.tgb.util.Cache   
 * @Description: TODO  
 * @author: LUCKY    
 * @date:2015年11月28日 下午3:53:51   
 * @version V1.0     
 */
package com.tgb.util.Cache;

import java.util.Properties;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.event.CacheManagerEventListener;
import net.sf.ehcache.event.CacheManagerEventListenerFactory;

/**  
 * @ClassName: PlatformCacheManagerEventListenerFactory   
 * @Description: CacheManager监听工厂
 * @author: LUCKY  
 * @date:2015年11月28日 下午3:53:51     
 */
public class PlatformCacheManagerEventListenerFactory  extends CacheManagerEventListenerFactory{
	@Override
	public CacheManagerEventListener createCacheManagerEventListener(
			CacheManager cacheManager, Properties properties) {
		return new MyCacheManagerEventListener(cacheManager);
	}

}




  MyCacheManagerEventListener类

  该类用来监听CacheManage的创建和销毁操作

/**     
 * @FileName: MyCacheManagerEventListener.java   
 * @Package:com.tgb.util.Cache   
 * @Description: TODO  
 * @author: LUCKY    
 * @date:2015年11月28日 下午3:56:12   
 * @version V1.0     
 */
package com.tgb.util.Cache;

import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Status;
import net.sf.ehcache.event.CacheManagerEventListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**  
 * @ClassName: MyCacheManagerEventListener   
 * @Description: CacheManage监听的实现操作
 * @author: LUCKY  
 * @date:2015年11月28日 下午3:56:12     
 */
public class MyCacheManagerEventListener implements CacheManagerEventListener{
	private Log logger = LogFactory.getLog( MyCacheManagerEventListener.class );
	private final CacheManager cacheManager;
  
	public MyCacheManagerEventListener(CacheManager cacheManager) {
		
		this.cacheManager = cacheManager;
	}

	public void init() throws CacheException {
		if(logger.isInfoEnabled())
		
			logger.info("init Cache..............");
		
	}

	public Status getStatus() {
		  if(logger.isInfoEnabled()){
			  logger.info("the current Cache status is "+cacheManager.getStatus());
		  }
	    return cacheManager.getStatus();
	   }

	public void dispose() throws CacheException {
		if(logger.isInfoEnabled()){
			logger.info("despose Cache...........");
		}
	}

	public void notifyCacheAdded(String cacheName) {
		if(logger.isInfoEnabled()){
			logger.info("cacheAdded the cacheName is"+cacheName);
		}
	}

	public void notifyCacheRemoved(String cacheName) {
		
		if(logger.isInfoEnabled()){
			logger.info("removeAdded the cacheName is"+cacheName);
	}

}

  PlatformCacheExceptionHandlerFactory类

  异常处理类,主要用来记录CacheMange创建和销毁过程中异常的一些信息

/**     
 * @FileName: PlatformCacheExceptionHandlerFactory.java   
 * @Package:com.tgb.util.Cache   
 * @Description: TODO  
 * @author: LUCKY    
 * @date:2015年11月28日 下午3:47:29   
 * @version V1.0     
 */
package com.tgb.util.Cache;

import net.sf.ehcache.Ehcache;
import net.sf.ehcache.exceptionhandler.CacheExceptionHandler;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**  
 * @ClassName: PlatformCacheExceptionHandlerFactory   
 * @Description: Cache异常处理器 
 * @author: LUCKY  
 * @date:2015年11月28日 下午3:47:29     
 */
public class PlatformCacheExceptionHandlerFactory implements CacheExceptionHandler {

	 private Log logger = LogFactory.getLog( getClass() );
	public void onException(Ehcache ehcache, Object key, Exception exception) {
		//如果Cache发生异常的话,进行日志处理
		if(logger.isInfoEnabled()){
			logger.info("Cache named \" "+key+"\" throw exception:"+exception.getMessage());
		}
	}

}


Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐