RSS
热门关键字:  java  Ajax  JSP  JSF  Struts
当前位置 : 首页>Java>列表

java动态缓存技术:WEB缓存应用

来源: 作者: 时间:2007-09-19 点击:

* @param intervalTime 缓存的时间周期,小于等于0时不限制
* @param maxVisitCount 访问累积次数,小于等于0时不限制
* @return
*/
public Object getCacheData(Object o, String methodName,Object[] parameters,
long intervalTime, int maxVisitCount) ...{
Class oc = o instanceof Class ? (Class)o : o.getClass();
StringBuffer key = new StringBuffer(oc.getName()); //生成缓存key值
key.append("-").append(methodName);
if (parameters != null) ...{
for (int i = 0; i < parameters.length; i++) ...{
if (parameters[i] instanceof Object[]) ...{
key.append("-").append(Arrays.toString((Object[])parameters[i]));
} else ...{
key.append("-").append(parameters[i]);
}
}
}

CacheData cacheData = (CacheData)cacheMap.get(key.toString());
if (cacheData == null) ...{//等待加载并返回
Object returnValue = invoke(o, methodName, parameters, key.toString());
return returnValue instanceof Class ? null : returnValue;
}
if (intervalTime > 0 && (System.currentTimeMillis() - cacheData.getTime()) > intervalTime) ...{
daemonInvoke(o, methodName, parameters, key.toString()); //缓存时间超时,启动线程更新数据
} else if (maxVisitCount > 0 && (maxVisitCount - cacheData.getCount()) <= 0) ...{//访问次数超出,启动线程更新数据
daemonInvoke(o, methodName, parameters, key.toString());
} else ...{
cacheData.addCount();
}
return cacheData.getData();
}

/** *//**
* 递归调用给定方法更新缓存中数据据
* @param o
* @param methodName
* @param parameters
* @param key
* @return 若反射调用方法返回值为空则返回该值的类型
*/
private Object invoke(Object o, String methodName,Object[] parameters, String key) ...{
Object returnValue = null;
try ...{
Class[] pcs = null;
if (parameters != null) ...{
pcs = new Class[parameters.length];
for (int i = 0; i < parameters.length; i++) ...{
if (parameters[i] instanceof MethodInfo) ...{//参数类型是MethodInfo则调用该方法的返回值做这参数
MethodInfo pmi = (MethodInfo)parameters[i];
Object pre = invoke(pmi.getO(), pmi.getMethodName(), pmi.getParameters(), null);
parameters[i] = pre;
}
if (parameters[i] instanceof Class) ...{
pcs[i] = (Class)parameters[i];
parameters[i] = null;
} else ...{
pcs[i] = parameters[i].getClass();
}
}
}
Class oc = o instanceof Class ? (Class)o : o.getClass();
// Method m = oc.getDeclaredMethod(methodName, pcs);
Method m = matchMethod(oc, methodName, pcs);
returnValue = m.invoke(o, parameters);
if (key != null && returnValue != null) ...{
addCacheData(key, returnValue, false);
}
if (returnValue == null) ...{
returnValue = m.getReturnType();
}
} catch(Exception e) ...{
log.error("调用方法失败,methodName=" + methodName);
if (key != null) ...{
removeCacheData(key);
log.error("更新缓存失败,缓存key=" + key);
}
e.printStackTrace();
}
return returnValue;
}

/** *//**
* 找不到完全匹配的方法时,对参数进行向父类匹配
* 因为方法aa(java.util.List) 与 aa(java.util.ArrayList)不能自动匹配到
*
* @param oc
* @param methodName
* @param pcs
* @return
* @throws NoSuchMethodException
* @throws NoSuchMethodException
*/
private Method matchMethod(Class oc, String methodName, Class[] pcs
) throws NoSuchMethodException, SecurityException ...{
try ...{
Method method = oc.getDeclaredMethod(methodName, pcs);
return method;
} catch (NoSuchMethodException e) ...{
Method[] ms = oc.getDeclaredMethods();
aa:for (int i = 0; i < ms.length; i++) ...{
if (ms[i].getName().equals(methodName)) ...{
Class[] pts = ms[i].getParameterTypes();
if (pts.length == pcs.length) ...{
for (int j = 0; j < pts.length; j++) ...{
if (!pts[j].isAssignableFrom(pcs[j])) ...{

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
Google Adsense
相关文章