How to cache a Memcached connection using the java spymemcached client
NickName:qualebs Ask DateTime:2013-08-12T14:40:39

How to cache a Memcached connection using the java spymemcached client

I am learning how to cache objects using memcached with the spymemcached client from spymemcached examples

MemcachedClient c=new MemcachedClient(new InetSocketAddress("hostname", portNum));    
// Store a value (async) for one hour
c.set("someKey", 3600, someObject);
// Retrieve a value (synchronously).
Object myObject=c.get("someKey");

I have noted that each time I want to cache or retrieve an object I create a new memcached client which am assuming is a new connection and that memcached has no connection pooling mechanism therefore users are advised to cache the connections to decrease overhead for reconnecting from this question opening closing and reusing connections.

My question is how do I cache this connection? Can someone please give me an example I can start from. If you are wondering what I have tried, I tried to put my connection in the memcached but then I realized that I have to create a connection to get it. :)

Thanks in advance.

Copyright Notice:Content Author:「qualebs」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/18180918/how-to-cache-a-memcached-connection-using-the-java-spymemcached-client

Answers
raffian 2013-08-12T18:54:18

\n I have noted that each time I want to cache or retrieve an object I\n create a new memcached client which am assuming is a new connection\n\n\nDon't do this; spymemcache uses a single connection for ALL I/O to and from memcache; it's all done asychronously; from spymemcache docs...\n\n\n Each MemcachedClient instance establishes and maintains a single\n connection to each server in your cluster.\n\n\nJust do the following once in your app; make sure the client is available to other services in your app so they can access it.\n\nMemcachedClient memClient = new MemcachedClient(new InetSocketAddress(host, port)); \n\n\nUse memClient for I/O with memcache; no need to create a new instance of MemcachedClient each time; done. The link you provided answers all of your questions.\n\nWhat is your deployment? web-app or standalone?",


mikewied 2013-08-12T18:40:23

This just means that you should use reuse the connections that you open as opposed to opening a connection for each request. It doesn't make sense to store a connection instance in memcached.\n\nCacheing the connection in the case means caching it in your application (keeping it in memory and open), not actually storing the connection in memcached.",


More about “How to cache a Memcached connection using the java spymemcached client” related questions

How to cache a Memcached connection using the java spymemcached client

I am learning how to cache objects using memcached with the spymemcached client from spymemcached examples MemcachedClient c=new MemcachedClient(new InetSocketAddress("hostname", portNum)); //...

Show Detail

How to connection pool memcached in Java (spymemcached)

The API I'm using, spymemcached, basically gives MemcachedClient memc = new MemCachedClient(new InetSocketAddress("host", port)); It doesn't really give me a connect() or isConnected() function. ...

Show Detail

spymemcached (Java Memcached Client)

Is there a way to collect the memcached stats using this API (spymemcached)? I know there are tools like cacti to collect information on Memcached Server. But I would like to collect, say, memory ...

Show Detail

How to add memcached nodes dynamically with spymemcached

I have a Java application setup which has multiple memcached server nodes communicating with a spymemcached client. I want to know if it is possible to add or remove server nodes at runtime, without

Show Detail

how to swap between two cache keys with CAS with memcached / spymemcached

I have a cache object (large json object) associated with a key. I would like to switch between this cache object / key instance and another without any down time in availability in one of the two...

Show Detail

Java (spymemcached) and PHP (memcached)

I'm trying to build a Java app that will set data in Memcached and have a PHP script to get the same data later in the process. So far, I haven't been able to find a way to get a 100% hit rate. To ...

Show Detail

How to access Infinispan Memcached Server cache container from a Spymemcached Client?

I've created a distributed cluster using Memcached over Infinispan. Now, I need to access to my cache using SpyMemcached client. I tried this : AuthDescriptor ad2 = new AuthDescriptor(new String...

Show Detail

Adapting spymemcached Java client GetFuture to a Guava ListenableFuture

I am looking for a Java memcached client that allows me perform asynchronous gets, preferably using Guava's ListenableFuture. One way that might be possible is using Spymemcached. The memcachedCl...

Show Detail

Items set with spymemcached cannot be fetched with php memcached

I am using spymemcached. I set a couple of items. Then I run a php script, however then I cannot get all those items using php memcached. PHP-Memcached can only partially retrieve those items. I c...

Show Detail

Migrating Memcached client from Memcached-Java-Client to Xmemcached

I have an old memcached client implementation that is based of the danga client, but was slightly modified. I think this client is now called Memcached-Java-Client. The implementation I have at ha...

Show Detail