Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memcached getting null for String set with python and then get from Java

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

like image 332
jckdnk111 Avatar asked Feb 05 '10 19:02

jckdnk111


People also ask

What is the basic syntax of Memcached set command?

The basic syntax of Memcached set command is as shown below − The keywords in the syntax are as described below − key − It is the name of the key by which data is stored and retrieved from Memcached.

What is the difference between stored and error in Memcached?

STORED indicates success. ERROR indicates incorrect syntax or error while saving data. In the following example, we use tutorialspoint as the key and set value Memcached in it with an expiration time of 900 seconds. To set a key in Memcached server, you need to use Memcached set method.

How do I connect to memcached in Python?

My preferred Python library for interacting with memcached is pymemcache —I recommend using it. You can simply install it using pip: The following code shows how you can connect to memcached and use it as a network-distributed cache in your Python applications:

How does Memcached solve concurrency problems?

When communicating with a remote cache, the usual concurrency problem comes back: there might be several clients trying to access the same key at the same time. memcached provides a check and set operation, shortened to CAS, which helps to solve this problem. The simplest example is an application that wants to count the number of users it has.


1 Answers

The solution right from documentation:

If you need to support multiple clients (i.e. Java, PHP, Perl, etc.) you need to make a few changes when you are setting things up:

// use a compatible hashing algorithm
pool.setHashingAlg( SockIOPool.NEW_COMPAT_HASH );

// store primitives as strings
// the java client serializes primitives
//
// note: this will not help you when it comes to
// storing non primitives
mcc.setPrimitiveAsString( true );

// don’t url encode keys
// by default the java client url encodes keys
// to sanitize them so they will always work on the server
// however, other clients do not do this
mcc.setSanitizeKeys( false );
like image 188
Dmytro Leonenko Avatar answered Oct 20 '22 00:10

Dmytro Leonenko