【问题标题】:memcached server return null value when retrive java objects value from memcached server从 memcached 服务器检索 java 对象值时,memcached 服务器返回空值
【发布时间】:2013-09-20 06:29:01
【问题描述】:

我进行了 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 类实现了它。

  1. CacheSessionImplementer.java
  2. 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);
    }
}
  1. CacheSessionImplementer.java --AbstractSessionObject :::: 的Memcache 实现者,用于从Memcached 中获取。 下面的代码“public Object getAttribute(), CacheSessionImplementer.java”从“public Object getAttribute(), RezSesion.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 卡住了。所以我期待好的解决方案。 谢谢。

【问题讨论】:

    标签: java caching memcached


    【解决方案1】:

    这些类是否实现了java.io.Serializable?您只能在 MemCache 中保留序列化对象,因为它将对象存储在辅助内存中。

    【讨论】:

    • 感谢您的快速回复。是的,我在 CacheSessionImplementer、RezSession、AbstractSessionObject 和 EngineFactory 以及一些主要的操作类中实现了 java.io.Serializable。我需要实现所有与 memcache 相关的 java 类吗?
    • 你好 Nazoordeen。您存储的这些类的对象是 Memcached 吗?还是他们不同?我可以看到这些类用于从缓存/会话中检索数据。
    • 嗨 Raghavendra,我将其存储在 memcached 中,该过程运行良好。但是当我从 memcached 中获取时,我会收到 memdown 错误,并且返回 null 值。我正在使用 CacheSessionImplementer.java 中的代码从 mecache 中检索数据。我用代码块更新了上面的问题。再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多