php - Predis - 如何使用带有 zadd 函数的数组?

标签 php caching redis predis

我刚开始使用 Predis 进行 Redis 迁移,但我无法让 zadd 函数处理数组。

此代码有效:

foreach ($userIndexArr as $row) {
  $usernames[] = 0;
  $usernames[] = $row['username']; 
  $result = $this->cache->zadd('@person', 0, $row['username']);
}

这不是:

foreach ($userIndexArr as $row) {
  $usernames[] = 0;
  $usernames[] = $row['username']; 
}
try {
    $result = $this->cache->zadd('@person', $usernames);
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}           

并且不会抛出任何错误。非常感谢任何帮助!

-J

最佳答案

我试过这个,如果你正在努力解决这个问题,下面的例子肯定会有所帮助(在 redis.io documents 例子之后):

$predis->zadd( 'myset', [ "one" => 1, "uno" => 1, "two" => 2, "three" => 3 ] )

这将产生与 redis 示例相同的排序集:

ZADD myzset 1 "one"
ZADD myzset 1 "uno"
ZADD myzset 2 "two" 3 "three"

如果你想在 Redis 中的一行中执行此操作,那么棘手的部分是将分数放在第一位,如下所示:

ZADD myzset 1 "one" 1 "uno" 2 "two" 3 "three"

在 Predis 中,这也可以工作:

$predis->zadd( 'myset', 1, "one", 1, "uno", 2, "two", 3, "three" );

关于php - Predis - 如何使用带有 zadd 函数的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26477074/

相关文章:

redis - 如何知道redis命令是写还是只读?

Redis 自愈集群或云、Kubernetes 或 Swarm 中的替代方案?

php - 需要时间戳帮助的日期

php - 从现有选项中预选择“选择”或多选选项,而不是添加和选择新选项

php - 如何缓存基于位置的数据库查询?

mysql - 如何频繁同步市场数据并显示为历史时间序列数据

使用 SSL 证书身份验证的 PHP TCP 连接到 EPP API

php - 网页上相关产品的提取价格

c# - 有什么方法可以缓存 Azure Cosmos DB 的 DocumentClient 以获得更好的性能?

linux - Artifactory 缓存文件显示的大小比其源文件大得多