import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; /** * RSS cache class implemented by Singleton * Function to cache the items of RSS, and the content of item * @author Smith He */ public final class RSSCacheUtils {
private CacheManager cacheManager = null;
private static RSSCacheUtils instance = null;
public static final String CACHE_NAME = "com.infindo.rssCache"; public static final String CONFIG_FILE = "/ehcache-rss.xml";
private RSSCacheUtils() { // InputStream is = this.getClass().getResourceAsStream("ehcache-rss.xml"); URL url = getClass().getResource(CONFIG_FILE); cacheManager = new CacheManager(url); }
public static RSSCacheUtils getInstance() { if(instance == null) { synchronized (RSSCacheUtils.class) { instance = new RSSCacheUtils(); }
} return instance; }
public Cache getRSSCache() { return this.cacheManager.getCache(CACHE_NAME); }
public void clearCache() { this.cacheManager.clearAll(); }
public void closeCache() { this.cacheManager.shutdown(); }
public void addElementToCache(Object key, Object value) { Element element = new Element(key, value); RSSCacheUtils.getInstance().getRSSCache().put(element); }
public Object getValueByKey(Object key) { Element element = RSSCacheUtils.getInstance().getRSSCache().get(key); if(element != null) { return element.getObjectValue(); } return null; } }