ehcachepool.java

来自「羽量级数据持久层开发框架」· Java 代码 · 共 65 行

JAVA
65
字号
package org.speedframework.cache;



import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.speedframework.exception.SpeedFrameworkException;

/**
 * ehcache pool implementation
 * 
 * @author lizf
 * 
 */
public class EhCachePool implements CachePool {

	/**
	 * 
	 */

	private static Cache cache = null;

	public EhCachePool(String region) {
		try {
			if (cache == null) {
				CacheManager manager = CacheManager.create();
				manager.addCache(region);
				cache = manager.getCache(region);
			}
		} catch (CacheException e) {
			throw new SpeedFrameworkException(
					"EhCachePool newInstance error. ", e);
		}
	}

	public synchronized void put(Object name, Object value) throws CacheException {
		Element element = new Element(name, value);
		cache.put(element);
	}

	public synchronized Object get(Object name) {
		Element element = cache.get(name);
		if (element != null) {
			return element.getValue();
		}
		return null;
	}

	public String getStats() {
		return cache.getStatistics().toString();
	}


	public void dispose() {
		cache.dispose();
	}

	public synchronized void remove(Object name) throws CacheException {
		cache.remove(name);
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?