我正在尝试通过别名使用 Predis 分片,如此处所述。我的代码基本相同,但我只返回空数组。我的哈希键周围需要 {} 吗?(编辑:不,刚刚尝试过)
$api->get("/test", function () {
$servers = [
["alias" => "metadata", "port" => 6380],
["alias" => "relations", "port" => 6381],
["alias" => "dim_provider", "port" => 6382],
["alias" => "dim_revctrcode", "port" => 6383],
["alias" => "dim_enccode", "port" => 6384],
["alias" => "dim_pos", "port" => 6385]
];
$options = [
"nodehash" => function ($connection) { return $connection->getParameters()->alias; },
"cluster" => function ($options) {
$replicas = Predis\Cluster\Distribution\HashRing::DEFAULT_REPLICAS;
$hashring = new Predis\Cluster\Distribution\HashRing($replicas, $options->nodehash);
$cluster = new Predis\Connection\PredisCluster($hashring);
return $cluster;
}
];
$redis = new Predis\Client($servers, $options);
try {
$test = $redis->scard("dim_provider");
print_r($test); // Prints 0 for scard or empty Array for hgetall
} catch (Exception $e) {
print $e->getMessage();
}
$redis = new Predis\Client(["port" => 6382]);
$test = $redis->scard("dim_provider");
print_r($test); // Works.
});
编辑:如果我只在$servers
阵列中放置一台服务器,它也可以工作。因此,散列似乎无法正常工作。当我在返回值前面抛出一些回声时,nodehash
我可以看到它正在返回别名。