代码之家  ›  专栏  ›  技术社区  ›  jckdnk111

MycCurp用Python获得字符串集的NULL,然后从Java获取

  •  8
  • jckdnk111  · 技术社区  · 14 年前

    当我试图从memcached读取我在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告诉我是不存在的,当我试图得到它时,它显然是空的:

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

    我猜这可能与跨语言的对象序列化/取消序列化有关,但我想我可以使用简单的字符串-以前有人遇到过这种情况吗?

    以下是我正在使用的libs:

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

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

    5 回复  |  直到 9 年前
        1
  •  4
  •   Dmytro Leonenko    14 年前

    文档中的解决方案:

    如果需要支持多个 客户端(即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 );
    
        2
  •  2
  •   user4179639    9 年前

    更改为使用 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
    
        3
  •  1
  •   Simon Callan    14 年前

    Java不使用Unicode吗?如果是这样,我怀疑python正在使用ascii/latin 1字符集写入memcache。因此,键看起来非常不同(“test_string”与“t\00e\00s\00t\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")
    
        4
  •  1
  •   Nishant Nawarkhede    10 年前

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

        5
  •  1
  •   sibnick    9 年前

    它是Java客户端中的bug。您应该获得带有修复的分叉项目: https://github.com/zmokhtar/Memcached-Java-Client

    编辑

    我可以用GWHALIN /MeMcCaskJava客户端复制问题,但是ZMKHTAR/MeMcCaskJava客户端的一切都很好