8

我正在使用 spymemcached。我设置了几个项目。然后我运行一个 php 脚本,但是我无法使用 php memcached 获取所有这些项目。PHP-Memcached 只能部分检索这些项目。

我无法更改 php 的散列算法或分发策略。在我们的系统中,我们使用默认散列(根据 php.net 文档,这是 jenkin 的一次一个)。分配策略是 php-memcached 的模数。我读过 spymemcached 使用一致的哈希。有什么方法可以在 spymemcached 中使用模散列。

换句话说,我怎样才能使 spymemcached 的 set 操作或任何其他与 php-memcached 的 get 操作兼容的存储操作?

如果 spymemcached 无法做到这一点,那么 java 中是否还有其他 memcached 客户端允许我这样做?

帮助不仅会受到赞赏,还会获得赏金。

Java代码:

public static void main(String [] args) {
    List<InetSocketAddress> addrs = new ArrayList<>();
    addrs.add(new InetSocketAddress("10.90.12.87", 11211));
    addrs.add(new InetSocketAddress("10.90.12.87", 11311));
    try {
        MemcachedClient memcache = new MemcachedClient(addrs);
        memcache.add("foo", 0, "bar");
        memcache.add("sample", 0, "key");
        memcache.add("try", 0, "another");
        memcache.add("ax-spadg-list", 0, "3045,6645");
    } catch (IOException ex) {
        Logger.getLogger(CategoryDataOperator.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println("Done");
}

PHP代码:

<?php
$mem = new Memcached();
$mem->addServer('10.90.12.87', 11211);
$mem->addServer('10.90.12.87', 11311);
var_dump $mem->get('foo');
var_dump($mem->get('try'));
var_dump($mem->get('sample'));
var_dump($mem->get('ax-spadg-list'));
4

3 回答 3

9

问题是关于哈希的,默认的 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");
    }
}
于 2013-10-03T09:07:34.810 回答
3

spymemcached 支持的哈希算法在这里:https ://github.com/couchbase/spymemcached/blob/master/src/main/java/net/spy/memcached/DefaultHashAlgorithm.java

您应该能够通过使用 ConnectionFactory 创建您的 MemcachedClient 来更改哈希算法。做这样的事情:

ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder();
builder.setHashAlgorithm(HashAlgorithm.CRC_HASH);
ConnectionFactory factory = builder.build();
MemcachedClient client = new MemcachedClient(Arrays.asList(new InetSocketAddr("localhost", 11211)), factory);
于 2013-09-25T21:29:06.257 回答
1

回复:Kakawait(以及 Shades88)

解决方案 #2 不正确,因为 xmemcached 没有正确移植使用未签名的 Jenkins 哈希的原始 C 代码。解决这个问题也将解决 Shades88 看到的 ArrayIndexOutOfBoundsException。

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

            // the hash variable in the original C code is a uint32.
            // convert the java signed int to an "unsigned",
            // represented via a long:
            return hash & 0xFFFFFFFFl;
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException("Hash function error", e);
        }
    }
}

// Unit test
public class JenkinsHashTest {
    @Test
    public void testHash() throws Exception {
        JenkinsHash j = new JenkinsHash();
        Properties p = new Properties();

        // This file contains k/v mappings,
        // with values generated by the reference C code
        p.load(new FileReader("src/test/resources/jenkinsHashTest.dat"));

        for (Entry<Object, Object> entry : p.entrySet()) {
            long result = j.hash((String)entry.getKey());
            // Print out hash mismatches
            if (result != Long.parseLong((String)entry.getValue())) {
                System.out.println("Key: " + (String)entry.getKey());
                System.out.println("Expected Hash Value: " + Long.parseLong((String)entry.getValue()));
                System.out.println("Actual Hash Value: " + result);
            }
            assertEquals(result, Long.parseLong((String)entry.getValue()));
        }
    }
}

测试数据文件用于将 Java 代码与 C 代码进行比较。构建 C 代码,然后散列一堆随机单词并将它们映射到一个文件中,如下所示:

jenkinsHashTest.dat:

sausage=2834523395
blubber=1103975961
pencil=3318404908
cloud=670342857
moon=2385442906
water=3403519606
computer=2375101981
school=1513618861
network=2981967937
hammer=1218821080

...根据需要添加任意数量

于 2014-01-03T18:17:48.460 回答