MemcachedClient and timeout

Massimo Marazza m.marazza at kataweb.it
Thu Mar 13 11:29:04 UTC 2008


Hello,

I'm new to this list, I've been looking for a solution to my problem
but I didn't found it, so please help me, any guidance would be very very
appreciated.

I'm using a java tag lib that in the doStartTag() method search a value
for a given key in the memcached sever. I'm using 
net.spy.memcached.MemcachedClient
and in the doStartTag() code I do something similar this:

  // hostcache = myserver1:11211
  MemcachedClient memcachedClient = new 
MemcachedClient(AddrUtil.getAddresses(hostcache));
  ....
  Object myObj = null;
  Future<Object> f = memcachedClient.asyncGet(mykey.toString());

  try {
    int timeOut = 100;
    myObj = f.get(timeOut, TimeUnit.MILLISECONDS);
    if (myObj == null){
      //try to get the resource without using memcached
      ...
    }else{
      System.out.println("content cached!!!");
    }
  }catch(TimeoutException e) {
    f.cancel(true);
    //try to get the resource without using memcached
    ...
  }
 
  Suppose I have to use this tag library in a web page X times, because
  that page references X articles that have to be cached. I'm using 
Tomcat 5.5
  java version "1.5.0_06".
  Suppose that the memcache server is down. During my tests, I noticed
  that when I get the page, the thread try to connect the server and the 
time
  to get page is T1 + <timeOut>*X, where T1 is the time to get other 
components,
  timeOut is the Timeout setting in the Future.get method and X is the 
number
  of taglib calls.
  1. Do I always have to wait for Timeout if the server is down?
     Is there a way to reduce this load time? Can the code realize
     the the server is down?
  2. Instead of creating a new MemcachedClient in the taglib class

     MemcachedClient memcachedClient = new 
MemcachedClient(AddrUtil.getAddresses(hostcache));
    
     I tried to get an istance of class MemcachedClient
     
     memcachedClient = (MemcachedClient) 
pageContext.getServletContext().getAttribute("memcachedClient");
     
     created at context startup in this way:
    
    public class ContextInitializer implements ServletContextListener {
       
       public void contextInitialized(ServletContextEvent event) {
          ServletContext context = event.getServletContext();
          String hostcache = context.getInitParameter("hostmemcached");

          try {
              context.setAttribute("memcachedClient", new 
MemcachedClient(AddrUtil.getAddresses(hostcache)));

          } catch (Throwable e) {
            System.out.println("MemcachedClient not Initialized");
            e.printStackTrace();
          }
      }
      
      public void contextDestroyed(ServletContextEvent event) {}
    }
      
      If the memcached server is down, the context doesn't start and 
I've noticed
      in the catalina log the tread trying to connect to memcached 
server. If the server is up
      I don't have problems.
      Is it correct to create a new MemcachedClient in the taglib? Could 
I have a java.lang.OutOfMemory Exception
      using a lot of taglib to cache contents? Why the context doesn't
      start when I try to create a single istance of MemcachedClient in 
ContextInitializer?


Thank you very much for your help!

Massimo


More information about the memcached mailing list