我进行了 memcached 实现并确定了移动到 memcache 的对象,但是从 memcached 获取它时遇到了一些问题。所以在我的应用程序中,(它是一个预订系统)我可以搜索并获取结果列表,但是当我选择酒店、航班或活动时,它会显示 memdown,memcach 错误。我列出了一些我使用的代码和方法,请帮助我解决这个问题。我非常感谢你的帮助,并在此先感谢。
我的应用程序是基于 Struts mvc 的。我在动作文件和一些jsp文件中使用了以下3种方法。我使用以下方法对识别的对象进行了内存缓存。
private RezSession rezSession = null;
//Moving identified objects to memcached server
private void setToCache(String attributeName, Object value,HttpServletRequest request) throws Exception {
rezSession = new RezSession();
rezSession.setAttribute(attributeName, value, request);
}
private Object getFromCache(String attributeName, HttpServletRequest request)throws Exception {
rezSession = new RezSession();
return (Object) rezSession.getAttribute(attributeName,request);
}
private void removeFromCache(String attributeName, HttpServletRequest request) throws Exception{
rezSession = new RezSession();
rezSession.removeAttribute(attributeName,request);
}
以上方法重定向到下面列出的 Rezsession 方法。AbstractSessionObject 是一个接口,两个 java 类实现了它。
- CacheSessionImplementer.java
- HttpSessionImplementer.java
RezSession.java 方法:-
public void setAttribute(String key,Object value, HttpServletRequest request) throws Exception {
HttpSession session = request.getSession();
try {
//AbstractSessionObject abstractSessionObject = RezSessionFactory.getInstance().getAbstractSessionObject();
if("ResPkgSearchForm".equals(key))
logger.debug(">>> Setting +:- "+key + " Class:"+(value==null?null:value.getClass()));
AbstractSessionObject abstractSessionObject = RezSessionFactory.getInstance().getSessionSpacificInstance(session);
abstractSessionObject.setAttribute(request,key,value,null);
} catch (Exception e) {
e.printStackTrace();
logger.fatal("error from new setAttribute",e);
}
}
public Object getAttribute(String key, HttpServletRequest request) throws Exception {
HttpSession session = request.getSession();
try {
//AbstractSessionObject abstractSessionObject = RezSessionFactory.getInstance().getAbstractSessionObject();
AbstractSessionObject abstractSessionObject = RezSessionFactory.getInstance().getSessionSpacificInstance(session);
Object value = abstractSessionObject.getAttribute(request,key,null);
if("ResPkgSearchForm".equals(key))
logger.debug(">>> Getting +:- "+key + " Class:"+(value==null?null:value.getClass()));
return value;
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("MEMDOWN", "Y");
logger.fatal("error from new getAttribute",e);
}
return null;
}
public void removeAttribute(String key, HttpServletRequest request) {
HttpSession session = request.getSession();
try {
AbstractSessionObject abstractSessionObject = RezSessionFactory.getInstance().getSessionSpacificInstance(session);
abstractSessionObject.removeAttribute(request,key,null);
} catch (Exception e) {
e.printStackTrace();
logger.debug("error from new removeAttribute",e);
}
}
CacheSessionImplementer.java --AbstractSessionObject :::: 的Memcache 实现者,用于从Memcached 中获取。下面的代码是从“public Object getAttribute(), RezSesion.java”调用的“public Object getAttribute(), CacheSessionImplementer.java”。
public Object getAttribute(HttpServletRequest request,String key,Object additionalParam) throws Exception{ SessionValueContainer sessionValueContainer = null; CacheResults cacheResults = null; String seqNo = ""; if (null != request.getParameter("seqNo") && !request.getParameter("seqNo").equals("") && !request.getParameter("seqNo").equalsIgnoreCase("null")) { seqNo = request.getParameter("seqNo"); } else if (null != request.getAttribute("seqNo")){ seqNo = request.getAttribute("seqNo").toString(); } String requestString = getRequestString(request.getSession().getId(),seqNo); Object value = getCachedValue(key,requestString); return value; }
这里 EngineFactory 正在选择应该选择的缓存引擎。
private Object getCachedValue(String key, String requestString) throws Exception, InstantiationException, IllegalAccessException, ClassNotFoundException { Object value = get(key, requestString); if (value != null && value instanceof String) { String new_mem_key = (String) value; // logger.debug("key ~>"+key+" --new_mem_key ~>"+new_mem_key); if ("MEM".equals(((String) value).split("#")[0])) { logger.debug("value before------------->" + value);// --getting // values // correctly---1 value = factory.getCacheEngine(memCacheKey).getObject( new_mem_key);// value is ok and redirect to // EngineFactory---2 logger.debug("value After------------->" + value);// --getting // null for // value---3 if (value == null) { logger.debug("MEMCACH-ERROR"); throw new NullPointerException("MEMDOWN"); } } } return value; }
就我而言,当我选择酒店、航班或活动时,在 getCachedValue() 方法中(上一个)
logger.debug("value before------------->"+value);//-----------------------------getting values correctly---1
value = factory.getCacheEngine(memCacheKey).getObject(new_mem_key);//value is ok and redirect to EngineFactory---2
logger.debug("value After------------->"+value);//-------------------------------getting null for value---3
所以我无法从缓存中获取值。我在我的应用程序中,将 fromsession 移动到 memcached 服务器很好,但是 getFromCache 卡住了。所以我期待好的解决方案。谢谢你。