本文共 4280 字,大约阅读时间需要 14 分钟。
缓存部分源码
抽象接口
public interface Cache
{
public Object get(Object key) throws CacheException;
public void put(Object key, Object value) throws CacheException;
public void update(Object key, Object value) throws CacheException;
public List keys() throws CacheException ;
public void remove(Object key) throws CacheException;
public void clear() throws CacheException;
public void destroy() throws CacheException;
}
public interface CacheProvider
{
public Cache buildCache(String regionName, boolean autoCreate) throws CacheException;
public void start() throws CacheException;
public void stop();
}
Ehcache支持
public class EhCache implements Cache
{
private net.sf.ehcache.Cache cache;
public EhCache(net.sf.ehcache.Cache cache)
{
this.cache = cache;
}
@Override
public Object get(Object key) throws CacheException
{
try
{
if (key == null)
return null;
else
{
Element element = cache.get(key);
if (element != null)
return element.getObjectValue();
}
return null;
}
catch (net.sf.ehcache.CacheException e)
{
throw new CacheException(e);
}
}
@Override
public void put(Object key, Object value) throws CacheException
{
try
{
Element element = new Element(key, value);
cache.put(element);
}
catch (IllegalArgumentException e)
{
throw new CacheException(e);
}
catch (IllegalStateException e)
{
throw new CacheException(e);
}
catch (net.sf.ehcache.CacheException e)
{
throw new CacheException(e);
}
}
@Override
public void update(Object key, Object value) throws CacheException
{
put(key, value);
}
@Override
@SuppressWarnings("unchecked")
public List keys() throws CacheException
{
return this.cache.getKeys();
}
@Override
public void remove(Object key) throws CacheException
{
try
{
cache.remove(key);
}
catch (IllegalStateException e)
{
throw new CacheException(e);
}
catch (net.sf.ehcache.CacheException e)
{
throw new CacheException(e);
}
}
@Override
public void clear() throws CacheException
{
try
{
cache.removeAll();
}
catch (IllegalStateException e)
{
throw new CacheException(e);
}
catch (net.sf.ehcache.CacheException e)
{
throw new CacheException(e);
}
}
@Override
public void destroy() throws CacheException
{
try
{
cache.getCacheManager().removeCache(cache.getName());
}
catch (IllegalStateException e)
{
throw new CacheException(e);
}
catch (net.sf.ehcache.CacheException e)
{
throw new CacheException(e);
}
}
}
public class CacheManager
{
private final static Logger log = LoggerFactory.getLogger(CacheManager.class);
private static CacheProvider provider;
static
{
initCacheProvider("org.express.portal.cache.EhCacheProvider");
}
private static void initCacheProvider(String prv_name)
{
try
{
CacheManager.provider = (CacheProvider) Class.forName(prv_name).newInstance();
CacheManager.provider.start();
log.info("Using CacheProvider : " + provider.getClass().getName());
}
catch (Exception e)
{
log.error("Unabled to initialize cache provider:" + prv_name + ", using ehcache default.", e);
CacheManager.provider = new EhCacheProvider();
}
}
private final static Cache _getCache(String cache_name, boolean autoCreate)
{
if (provider == null)
{
provider = new EhCacheProvider();
}
return provider.buildCache(cache_name, autoCreate);
}
/**
* 获取缓存中的数据
*
* @param name
* @param key
* @return
*/
public final static Object get(String name, Serializable key)
{
if (name != null && key != null)
return _getCache(name, true).get(key);
return null;
}
/**
* 获取缓存中的数据
*
* @param
* @param resultClass
* @param name
* @param key
* @return
*/
@SuppressWarnings("unchecked")
public final static T get(Class resultClass, String name, Serializable key)
{
if (name != null && key != null)
return (T) _getCache(name, true).get(key);
return null;
}
/**
* 写入缓存
*
* @param name
* @param key
* @param value
*/
public final static void set(String name, Serializable key, Serializable value)
{
if (name != null && key != null && value != null)
_getCache(name, true).put(key, value);
}
/**
* 清除缓冲中的某个数据
*
* @param name
* @param key
*/
public final static void evict(String name, Serializable key)
{
if (name != null && key != null)
_getCache(name, true).remove(key);
}
/**
* 清除缓冲中的某个数据
*
* @param name
* @param key
*/
public final static void justEvict(String name, Serializable key)
{
if (name != null && key != null)
{
Cache cache = _getCache(name, false);
if (cache != null)
cache.remove(key);
}
}
}
转载地址:http://iphhv.baihongyu.com/