【问题标题】:Memcached getting null for String set with python and then get from JavaMemcached 为使用 python 设置的字符串获取 null 然后从 Java 获取
【发布时间】:2011-01-13 16:05:14
【问题描述】:

当我尝试从我在 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 告诉我 is 不存在,当我尝试获取它时显然返回 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

【问题讨论】:

  • 您应该在 memcached 服务器上进行详细日志记录,并确保您认为正在发生的事情实际上正在发生。
  • 好建议,但我实际上无权访问这个特定的 memcached 服务器。我尝试了这个其他 java 库,它似乎按预期工作:code.google.com/p/spymemcached 我仍然想知道为什么其他库不工作 - 只是好奇。
  • 它可能无法理解返回的值。你可以配置 spymemcached 来模拟 Whalin 的转码器,看看它是否也失败了。
  • @jckdnk111 你是怎么解决的?
  • @Gank,我检查了这个场景,它对我有用。您正在使用哪些库/版本?

标签: java python memcached


【解决方案1】:

Java 不使用 unicode 吗?如果是这样,我怀疑 python 正在使用 ASCII / latin 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")

【讨论】:

  • 好主意,不幸的是 python lib 无法处理 unicode 键:memcache.MemcachedStringEncodingError: Keys must be str()'s, not unicode。使用 mystring.encode(charset) 转换您的 unicode 字符串 我也尝试了 mystring.encode("utf-8") 方法,虽然它摆脱了 python 异常,但并没有解决问题。
  • 我认为 Java 使用 utf-16 进行本机编码,因此请尝试使用 'utf-16'、'utf-16le' 和 'utf-16be' 作为字符集。 le/be 选项可能需要也可能不需要。
  • /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/python_memcached-1.58-py3.4.egg/memcache.py",第 1077 行,在_get if server.connect(): 文件“/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/python_memcached-1.58-py3.4.egg/memcache.py”,第 1065 行, in _unsafe_get if isinstance(msg, tuple): 文件“/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/python_memcached-1.58-py3.4.egg/memcache.py” ,第 1257 行,在 _recv_value def check_key(self, key, key_extra_len=0): ValueError: Unknown flags on get: 200
【解决方案2】:

文档中的解决方案:

如果您需要支持多个 客户端(即 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 );

【讨论】:

【解决方案3】:

确保memcached服务已经启动,如果已经启动则重启服务。

【讨论】:

    【解决方案4】:

    这是您的 Java 客户端中的错误。你应该得到修复的分叉项目:https://github.com/zmokhtar/Memcached-Java-Client

    编辑

    我可以重现 gwhalin/Memcached-Java-Client 的问题,但使用 zmokhtar/Memcached-Java-Client 一切正常

    【讨论】:

      【解决方案5】:

      改用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
      

      【讨论】:

        猜你喜欢
        • 2014-04-29
        • 2015-07-13
        • 1970-01-01
        • 2011-12-09
        • 1970-01-01
        • 2013-08-28
        • 2013-10-25
        • 1970-01-01
        • 2021-12-21
        相关资源
        最近更新 更多