问题是关于哈希的,默认的 php-memcached 哈希是
(Jenkins 一次一个) 项目密钥散列算法
而 spymemcached 哈希列表是:
NATIVE_HASH
: 根本Native hash (String.hashCode()).
不匹配默认的 php-memcachedMemcached::HASH_DEFAULT
CRC_HASH
=>Memcached::HASH_CRC
FNV1_64_HASH
=>Memcached::HASH_FNV1_64
FNV1A_64_HASH
=>Memcached::HASH_FNV1A_64
FNV1_32_HASH
=>Memcached::HASH_FNV1_32
FNV1A_32_HASH
=>Memcached::HASH_FNV1A_32
KETAMA_HASH
=> "ketama 使用的基于 MD5 的哈希算法。" 所以也许Memcached::HASH_MD5
但无论如何不是Memcached::HASH_DEFAULT
因此,如果您无法更改 PHP 客户端配置或扩展 spymemcached 库,则两个库之间没有直接匹配。
解决方案 1:如果您查看历史记录(您可以使用 php 客户端哈希修改的示例)。
解决方案 2:否则您可以创建一个 JenkinsHash 类(我复制过去的 Xmemcached 代码:https ://code.google.com/p/xmemcached/source/browse/trunk/src/main/java/net/rubyeye/xmemcached/ HashAlgorithm.java?r=801#176 [但要考虑 Xmemcached 许可证并将作者/许可证保留在源代码中])
import net.spy.memcached.HashAlgorithm;
import java.io.UnsupportedEncodingException;
public class JenkinsHash implements HashAlgorithm {
@Override
public long hash(String k) {
try {
int hash = 0;
for (byte bt : k.getBytes("utf-8")) {
hash += (bt & 0xFF);
hash += (hash << 10);
hash ^= (hash >>> 6);
}
hash += (hash << 3);
hash ^= (hash >>> 11);
hash += (hash << 15);
return hash;
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Hash function error", e);
}
}
}
然后:
import net.spy.memcached.*;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
public static void main(String[] args) throws IOException {
List<InetSocketAddress> addrs = new ArrayList<InetSocketAddress>();
addrs.add(new InetSocketAddress("127.0.0.1", 11211));
addrs.add(new InetSocketAddress("172.28.29.22", 11211));
try {
ConnectionFactory connectionFactory = new ConnectionFactoryBuilder()
.setProtocol(ConnectionFactoryBuilder.Protocol.TEXT)
.setHashAlg(new JenkinsHash())
.setLocatorType(ConnectionFactoryBuilder.Locator.ARRAY_MOD).build();
MemcachedClient memcache = new MemcachedClient(connectionFactory, addrs);
memcache.add("foo", 0, "bar2");
memcache.add("sample", 0, "key");
memcache.add("try", 0, "another");
memcache.add("ax-spadg-list", 0, "3045,6645");
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Done");
}
}
使用 php 脚本:
<?php
$memcached = new Memcached();
$memcached->addserver('127.0.0.1', 11211);
$memcached->addserver('172.28.29.22', 11211);
var_dump($memcached->get('foo'));
var_dump($memcached->get('try'));
var_dump($memcached->get('sample'));
var_dump($memcached->get('ax-spadg-list'));
测试:
$ echo "flush_all" | nc 172.28.29.22 11211 && echo "flush_all" | nc 127.0.0.1 11211
OK
OK
$ php mem.php
bool(false)
bool(false)
bool(false)
bool(false)
RUN JAVA
$ php mem.php
string(4) "bar2"
string(7) "another"
string(3) "key"
string(9) "3045,6645"
解决方案 3:使用带有哈希算法的https://code.google.com/p/xmemcached/ONE_AT_A_TIME
import net.rubyeye.xmemcached.HashAlgorithm;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.impl.ArrayMemcachedSessionLocator;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException, MemcachedException, TimeoutException {
List<InetSocketAddress> addrs = new ArrayList<InetSocketAddress>();
addrs.add(new InetSocketAddress("127.0.0.1", 11211));
addrs.add(new InetSocketAddress("172.28.29.22", 11211));
MemcachedClientBuilder builder = new XMemcachedClientBuilder(addrs);
builder.setSessionLocator(new ArrayMemcachedSessionLocator(HashAlgorithm.ONE_AT_A_TIME));
MemcachedClient memcachedClient = builder.build();
memcachedClient.set("foo", 0, "bar2");
memcachedClient.set("sample", 0, "key");
memcachedClient.set("try", 0, "another");
memcachedClient.set("ax-spadg-list", 0, "3045,6645");
memcachedClient.shutdown();
System.out.println("Done");
}
}