Memcached在Python中设置字符串后在Java中获取为null

9 投票
5 回答
4732 浏览
提问于 2025-04-15 18:56

我在用Python设置一个字符串到memcached时,想从中读取这个字符串:

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:

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
        }
}

我猜这可能跟不同编程语言之间的对象序列化和反序列化有关,但我以为简单的字符串应该没问题——有没有人遇到过类似的情况?

这是我使用的库:

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

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

5 个回答

1

Java不是使用unicode吗?如果是这样的话,我怀疑Python在写入memcache时使用的是ASCII或拉丁1字符集。因此,键的显示方式会很不一样(比如“test_string”和“t\00e\00s\00t\00_\00s\00t\00r\00i\00n\00g\00”)。

试试这个,看看会发生什么。

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")
2

改用 pylibmc 来解决这个问题:

import pylibmc

mc = pylibmc.Client(["127.0.0.1"], binary=True,
                   behaviors={"tcp_nodelay": True,
                               "ketama": True})

key="someKey"
i=0
while True:
    #mc.set(key, str(i))
    value = mc.get(key)
    print(value)
    sleep(1)
    i+=1
4

这是文档中给出的解决方案:

如果你需要支持多个客户端(比如 Java、PHP、Perl 等),在设置的时候你需要做一些调整:

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

撰写回答