Memcached通过python为String设置为null,然后从Java获取 [英] Memcached getting null for String set with python and then get from Java

查看:133
本文介绍了Memcached通过python为String设置为null,然后从Java获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从我在python中设置的memcached中读取字符串时:

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告诉我它不存在,当我试图得到它时显然会返回null:

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我猜它与跨语言的对象序列化/非序列化有关但我觉得我对简单的字符串可能没问题 - 以前有人碰到这个吗?

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

推荐答案

解决方案直接来自文档:

The solution right from documentation:


如果你需要支持多个
客户端(即Java,PHP,Perl等)
当您设置
时需要进行一些更改:

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 );

这篇关于Memcached通过python为String设置为null,然后从Java获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆