views:

280

answers:

1

When I try to read a String from memcached that I set in python:

import memcache

MC_SERVER = "192.168.1.100"
MC_PORT = "11211"

mc = memcache.Client(['%s:%s' % (MC_SERVER, MC_PORT)], debug=0)
mc.set("test_string", "true")
print mc.get("test_string")

Java tells me is doesn't exist and obviously returns null when I try to get it:

import com.danga.MemCached.*;
public class Tester {

        // create a static client as most installs only need
        // a single instance
        protected static MemCachedClient mcc = new MemCachedClient(true, false);

        // set up connection pool once at class load
        static {

                // server list and weights
                String[] servers =
                        {
                          "192.168.1.100:11211"
                        };

                // grab an instance of our connection pool
                SockIOPool pool = SockIOPool.getInstance();

                // set the servers and the weights
                pool.setServers( servers );

                // set some TCP settings
                // disable nagle
                // set the read timeout to 3 secs
                // and don't set a connect timeout
                pool.setNagle( false );
                pool.setSocketTO( 3000 );
                pool.setSocketConnectTO( 0 );

                // initialize the connection pool
                pool.initialize();
        }

        // from here on down, you can call any of the client calls
        public static void main(String[] args) {
                //System.out.println( mcc.set( "test_string", "blah!" ) ); // everything is great is value is set by Java
                System.out.println( mcc.keyExists( "test_string" ) ); // output is false when value set by python
                System.out.println( mcc.get( "test_string" ) ); // output is null when value set by python
        }
}

I am guessing it has something to do with the Object serialization / un-serialization across languages but I thought I might be OK for simple Strings - anyone run into this before?

Here are the libs I am using:

http://www.tummy.com/Community/software/python-memcached/

http://github.com/gwhalin/Memcached-Java-Client/downloads

A: 

Doesn't Java use unicode? If so, I suspect that python is writing to memcache using the ASCII / latin 1 character set. As a result, the keys look very different ("test_string" vs. "t\00e\00s\00t\00_\00s\00t\00r\00i\00n\00g\00").

Try using this, and see what happens.

import memcache

MC_SERVER = "192.168.1.100"
MC_PORT = "11211"

mc = memcache.Client(['%s:%s' % (MC_SERVER, MC_PORT)], debug=0)
mc.set(u"test_string", u"true")
print mc.get(u"test_string")
Simon Callan
Good idea, unfortunately the python lib can't handle unicode keys: memcache.MemcachedStringEncodingError: Keys must be str()'s, not unicode. Convert your unicode strings using mystring.encode(charset)I tried the mystring.encode("utf-8") method as well and although it got rid of the python exception, it didn't fix the problem.
jckdnk111
I think Java uses utf-16 for its native encoding, so try 'utf-16', 'utf-16le' and 'utf-16be' for the charset. The le/be options may or may not be necessary.
Simon Callan