Thank you Henrik for you reply.
I followed your suggestion so I created a class like this (singleton)

public class MemcachedUtilityStatic {
private static MemcachedClient memcachedClient = null; public synchronized static MemcachedClient getIstance(String hostcache) throws Exception{
       if (memcachedClient == null) {
           try {
memcachedClient = new MemcachedClient(AddrUtil.getAddresses(hostcache));
           } catch(Exception e) {
               e.printStackTrace();
         }
       }
       return memcachedClient;
     }
}

and I used this class in my doStartTag method

public int doStartTag() throws JspException {
       try {
String hostcache = pageContext.getServletContext().getInitParameter("hostmemcached");
           memcachedClient = MemcachedUtilityStatic.getIstance(hostcache);
            ......
}

Suppose memcached server is up. No problem found when I start the context and I request the page, but if I stop the memcache server then I start it and I request the page, the MemcachedClient object is not able to cache
contents.
If I use a different approach in the tag lib, that is create a new MemcachedClient object public int doStartTag() throws JspException {
       try {
String hostcache = pageContext.getServletContext().getInitParameter("hostmemcached"); memcachedClient = new MemcachedClient(AddrUtil.getAddresses(hostcache));
            ......
}

and if I do a memcached server stop and start, after the restart the MemcachedClient is able to cache objects.
For me that's strange...
Maybe the right approach when the memcache server goes down is to restart the web container (Tomcat)?

Thanks in advance,

Massimo

Henrik Schröder ha scritto:
On Thu, Mar 13, 2008 at 12:29 PM, Massimo Marazza <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:

    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:

If you're using a heavier client with built-in socket pooling and as in SpyMemcached's case, a message queue, you should never create more than one instance, and you should always reuse it. Creating it once every time you need it means that you get the high start-up cost each and every time, and none of the benefits such as pooled sockets. you really, really want to avoid this. I'm sure SpyMemcached keeps track of non-working servers internally, but if you destroy the client after every request, you won't keep this knowledge, and each new request won't discover this until after trying to connect to the non-working server.

Instead, put the client as a static field in some class, and create it in the static initializer for that class, and then re-use that instance everywhere in your code. Something like this:

public class MyLittleCacheClass {
    public static MemcachedClient Client;
    static() {
        string hostcache = "myserver1:11211";
        Client = new MemcachedClient(AddrUtil.getAddresses(hostcache));
        //Add any further config settings here...
    }
}

...and then you do

Future<Object> f = MyLittleCacheClass.Client.asyncGet(mykey.toString());
[...]

when you want to use it like in your example.


/Henrik Schröder - apologies for any incorrect Java, I'm a little rusty.

Reply via email to