我是 memcached 的新手,正在认识一个老 friend Java - 我在 win x64 和 java 1.7 上运行。还通过安装文件 couchbase-server-enterprise_2.2.0_x86_64 在我本地的 win 64 机器上运行 couchbase memcache 服务器。一切正常,直到我注意到在与 telnet session 中设置的键进行字符串比较并在 java 中检查此键时出现奇怪的行为。

来自 telnet session

set s1 1 0 4 
abcd 
STORED 
set s2 32 0 4 
abcd 
STORED 

来 self 的主要 java 测试类: ...

System.out.println("Get s1 from Cache:" +mcca.get("s1")); 
System.out.println("Get s1 from Cache:" +mcca.get("s1",1)); 
System.out.println("Get s1 from Cache:" +mcca.get("s1",32)); 
System.out.println("Get s1 from Cache:" +mcca.get("s1",77, true)); 
 
System.out.println("Get s2 a from Cache:" +mcca.get("s2")); 
System.out.println("Get s2 b from Cache:" +mcca.get("s2",1));           
System.out.println("Get s2 c from Cache:" +mcca.get("s2",32)); 
System.out.println("Get s2 c from Cache:" +mcca.get("s2",77, true)); 

输出

Get s1 from Cache:97 
Get s1 from Cache:97 
Get s1 from Cache:97 
com.danga.MemCached.MemCachedClient Mon Dec 30 11:50:06 EST 2013 - ++++ retrieving object and stuffing into a string. 
Get s1 from Cache:abcd 
Get s2 a from Cache:abcd 
Get s2 b from Cache:abcd 
Get s2 c from Cache:abcd 
com.danga.MemCached.MemCachedClient Mon Dec 30 11:50:06 EST 2013 - ++++ retrieving object and stuffing into a string. 
Get s2 c from Cache:abcd 

我在看这里:http://www.geelou.com/javadocs/java_memcached-release_2.0.1/com/danga/MemCached/MemCachedClient.html但我没有看到任何关于 hashCode 的解释,也没有看到它是否对应于 memcached 服务器中的相同标志/元数据参数。

我想我的问题大致可以归结为:com.danga get 命令的 hashCode 参数值是否可以从 32 更改,这样当我设置带有元数据/标志 1 的 s1 键时,我可以得到完整的字符串,如上所示,而无需指定 asString 标志或 mcca.setPrimitiveAsString(true)?

与此相关,为什么

System.out.println("Get s2 a from Cache:" +mcca.get("s2"));  

打印似乎是 abcd 的正确值,而两者都不是:

System.out.println("Get s1 from Cache:" +mcca.get("s1")); 
System.out.println("Get s1 from Cache:" +mcca.get("s1",1)); 

打印出 abcd 的正确值是多少?

如本回答中所述 Memcached getting null for String set with python and then get from Java我可以使用

解决我的问题
mcca.setPrimitiveAsString(true); 
mcca.setSanitizeKeys(false); 
pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH); 

但我仍然不明白为什么会出现差异,以及是否/如何修改调用以获取参数来修复它

注意事项: 将标志/元数据参数设置为 32 的原因是在我运行的另一个小 java 测试中

System.out.println("set 1 status:" + mcc.set("1", "Modified")); 
 
//which outputs 
com.danga.MemCached.MemCachedClient Fri Dec 27 00:12:51 EST 2013 - ++++ memcache cmd (result code): set 1 32 0 8 
(STORED) 

这似乎表明 com.danga 库正在使用 32 的标志/元数据值。

我认为我的问题归结为来自内存缓存 telnet session

set s1 1 0 4  
abcd   
set s2 32 0 4  
abcd   

来自 java 为什么

mcca.get("s1")// only gives the first ascii character code (97)  
mcca.get("s2")// but gives the entire string.  What is so special about the second memcache command using the hash of 32? 

请您参考如下方法:

我认为问题在于您的“标志”参数告诉您的图书馆数据的存储方式不同。我不确定您为什么在 telnet SET 命令中指定“1”和“32”,但您可能想使用指定的不同标志值来测试它。

您指定的“1”和“32”通常由客户端库用来指定数据的格式。它们不是哈希值。


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!