1

I did memcached implementation and identified objects moved to memcache, but I have some problems with get it from memcached. So in my application, (it is a reservation system) I can search and get the Result list ,but when I select hotel,or Flight, or Activity it says memdown, memcach error. I listed some codes and methods I used and pls help me to solve this problem.I really appreciate your help and thanks in advance.

My app is Struts mvc based. and I used below 3 methods in action files, and some jsp files. I did memcaching identified objects , using below methods.

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);
}

Above methods redirect to Rezsession methods listed below. AbstractSessionObject is a interface , two of java classes implements it.

  1. CacheSessionImplementer.java
  2. HttpSessionImplementer.java

RezSession.java methods:-

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 --Memcache implementer of AbstractSessionObject :::: this method Used for get from Memcached. Below code "public Object getAttribute(), CacheSessionImplementer.java" called from "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;
    }
    

    Here EngineFactory is selecting whihc caching engine should be select.

    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;
    }
    

In my case , when I am selecting a hotel or flight or activity, in getCachedValue() method(Above one)

    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

So I can't get values from Cache. I In my app, Moving fromsession to memcached server is fine but, getFromCache is stuck. So I ecpecting good solutions. Thank you.

1 Answer 1

1

Are those classes implement java.io.Serializable? You can keep only Serialized Objects in MemCache, as it stores the objects in Secondary Memory.

3
  • Thank you for your quick reply. Yes, I implemented java.io.Serializable in CacheSessionImplementer,RezSession,AbstractSessionObject and EngineFactory, and some main action classes. do I need to implement to all java classes which relted to memcache.
    – Nazoordeen
    Sep 16, 2013 at 8:27
  • Hi Nazoordeen. Are objects of these classes that you are storing is Memcached? Or are they different? I could see these classes are used to retrieve the data from Cache/Session. Sep 16, 2013 at 9:11
  • Hi Raghavendra, I am storing this at memcached, that process is working fine. but when I'm getting from memcached, then I am getting memdown error, and null value return. I'm retriving data from mecache using codes in CacheSessionImplementer.java. I updated above Question with the code block. once again thank you.
    – Nazoordeen
    Sep 17, 2013 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.