On Thu, Mar 13, 2008 at 12:29 PM, Massimo Marazza <[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