package com.byd.mes.util.cache;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.byd.mes.util.BeanFactory;
public interface CacheManager {
/**
* 回调函数
*/
public interface CallBack{
Serializable callBack() throws Exception;
}
/**
* 获取缓存值
* @param cacheKey
* @return Object
*/
public Object get(String cacheKey);
/**
* 记录该缓存
* @param cacheKey
* @param callBack
* @return Object
*/
public Object cache(String cacheKey,CacheManager.CallBack callBack);
/**
* 根据KEY清空缓存
* @param cacheKey
*/
public void clear(String cacheKey);
/**
* 清空所有缓存
*/
public void clearAllCache();
/**
* 根据名称清空缓存
* @param tacticName
*/
public void clearCacheByTacticName(String tacticName);
/**
* 根据名称获取缓存
* @param tacticName
* @return List
*/
public List<Map> getCache(String tacticName);
}
package com.byd.mes.util.cache;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.byd.mes.bussiness.service.system.ext.CacheTacticHandle;
public class CacheManagerImpl implements CacheManager{
private static final Log log = LogFactory.getLog(CacheManagerImpl.class);
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private Cache cache;
private CacheTacticHandle cacheTacticHandle;
@Override
public Object get(String cacheKey){
return getCache().get(cacheKey);
}
@Override
public Object cache(String cacheKey,CacheManager.CallBack callBack){
String k = StringUtils.left(cacheKey, cacheKey.indexOf("@"));
if(!cacheKey.startsWith("system.cacheTacic") //强制开启缓存规则本身的缓存
&& !cacheTacticHandle.isEnable(k) //用户是否开启了相关的缓存规则
){
try{
log.debug("未开启缓存规则>>> "+cacheKey);
return callBack.callBack();
}catch(Exception ex){
throw new RuntimeException(ex);
}
}
Cache cache = getCache();
Element obj = cache.get(cacheKey);
Object result = null;
if(obj == null){
try{
result = callBack.callBack();
}catch(Exception ex){
throw new RuntimeException(ex);
}
Element element = new Element(cacheKey, result);
log.debug("设置缓存[cacheKey:"+cacheKey+",cacheValue:"+result+"]");
getCache().put(element);
}
else{
result = obj.getValue();
log.debug("数据来自缓存>>>"+result);
}
return result;
}
@Override
public void clear(String cacheKey){
log.debug("Clear cache >>>"+cacheKey);
boolean b = getCache().remove(cacheKey);
}
@Override
public void clearAllCache() {
getCache().removeAll();
}
@Override
public void clearCacheByTacticName(String tacticName) {
Cache cache = getCache();
List<String> keys = cache.getKeys();
for(String key : keys){
if(key.startsWith(tacticName)){
cache.remove(key);
}
}
}
@Override
public List<Map> getCache(String tacticName) {
List<Map> list = new ArrayList<Map>();
Cache cache = getCache();
List<String> keys = cache.getKeys();
for(String key : keys){
if(key.startsWith(tacticName)){
Map m = new HashMap();
Element e = cache.get(key);
m.put("key", key);
log.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+e.getValue());
m.put("value", e.getValue().toString());
m.put("createTime", format.format(new Date(e.getCreationTime())));
list.add(m);
}
}
return list;
}
public void setCache(Cache cache) {
this.cache = cache;
}
public Cache getCache() {
return cache;
}
public void setCacheTacticHandle(CacheTacticHandle cacheTacticHandle) {
this.cacheTacticHandle = cacheTacticHandle;
}
public CacheTacticHandle getCacheTacticHandle() {
return cacheTacticHandle;
}
}
package com.byd.mes.util.cache;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.byd.mes.commons.exception.NestedRuntimeException;
import com.byd.mes.view.action.ActionException;
public class CacheException extends NestedRuntimeException {
private static final Log log = LogFactory.getLog(CacheException.class);
public CacheException(String errorCode, String message) {
super(errorCode, message);
}
public CacheException(String errorCode,String message, Throwable cause) {
super(errorCode,message, cause);
}
public CacheException(String errorCode, Map<String,Object> args, String message, Throwable cause) {
super(errorCode,args,message, cause);
}
}