Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Redis 如何对集合进行排序并获取完整哈希_Redis_Phpredis - Fatal编程技术网

Redis 如何对集合进行排序并获取完整哈希

Redis 如何对集合进行排序并获取完整哈希,redis,phpredis,Redis,Phpredis,我是Redis的新手,我必须说我爱它直到现在:) 我遇到了一个问题,我不知道如何更有效地解决它。 我有一个集的散列。每个HASH描述一篇文章 下面是创建和存储散列的代码: // Create the HASH $key = 'post:'.$post->getId(); $this->redis->hSet($key, 'created', $post->getCreated()); $this->redis->hSet($key, 'author', $po

我是Redis的新手,我必须说我爱它直到现在:)

我遇到了一个问题,我不知道如何更有效地解决它。 我有一个
散列
。每个
HASH
描述一篇文章

下面是创建和存储
散列的代码:

// Create the HASH
$key = 'post:'.$post->getId();
$this->redis->hSet($key, 'created', $post->getCreated());
$this->redis->hSet($key, 'author', $post->getAuthor());
$this->redis->hSet($key, 'message', $post->getMessage());

// Store the HASH in the SET
$this->redis->sAdd('posts', $post->getId());
现在,之前我将帖子的所有属性存储在
散列
(json_编码)的
数据
字段中,并获取如下信息:

$key = 'posts';
$data = $this->redis->sort($key, array(
    'by' => 'nosort',
    'limit' => array($offset, $limit),
    'get' => 'post:*->data '
));

if (!is_array($data)) {
    return array();
}

foreach ($data as &$post) {
    $post = json_decode($post, true);
}
$key = 'posts';
$data = $this->redis->sort($key, array(
    'by' => 'nosort',
    'limit' => array($offset, $limit),
    'get' => array('post:*->created', 'post:*->author', 'post:*->message')
));
$key = 'posts';
$data = $this->redis->sort($key, array(
    'by' => 'nosort',
    'limit' => array(0, 10),
    'get' => 'post:*->data '
));
它工作得很好,我有所有的帖子信息:)

但是我在Redis中更新帖子时遇到了冲突(并发更新),所以我决定将帖子的所有属性都放在
散列的
字段中,这样就解决了我的冲突问题

现在的问题是从我的
集合
中获取
散列
。我是否必须这样指定每个字段:

$key = 'posts';
$data = $this->redis->sort($key, array(
    'by' => 'nosort',
    'limit' => array($offset, $limit),
    'get' => 'post:*->data '
));

if (!is_array($data)) {
    return array();
}

foreach ($data as &$post) {
    $post = json_decode($post, true);
}
$key = 'posts';
$data = $this->redis->sort($key, array(
    'by' => 'nosort',
    'limit' => array($offset, $limit),
    'get' => array('post:*->created', 'post:*->author', 'post:*->message')
));
$key = 'posts';
$data = $this->redis->sort($key, array(
    'by' => 'nosort',
    'limit' => array(0, 10),
    'get' => 'post:*->data '
));
或者是否有其他方法可以直接在
集合中获取完整的
散列

我听说了
pipeline
,但我不确定它是否是我想要的,是否可以与
phpredis一起使用

干杯,马克西姆


更新

我不确定我是否解释清楚了。我在一个集合中有一些元素(
post\u id
)。 我想要得到
集合的前10个post
,这意味着我想要10个
散列
(及其所有字段和值),以便构建
post
对象

我以前将所有对象信息存储在散列(
数据
)的一个字段中,现在对象的每个属性都有一个字段

之前:

myHash:data

现在:

myHash:id“1234”创建了“2010-01-01”作者“John”

在我使用
SORT
获取前10个帖子(并且可以轻松分页)之前,如下所示:

$key = 'posts';
$data = $this->redis->sort($key, array(
    'by' => 'nosort',
    'limit' => array($offset, $limit),
    'get' => 'post:*->data '
));

if (!is_array($data)) {
    return array();
}

foreach ($data as &$post) {
    $post = json_decode($post, true);
}
$key = 'posts';
$data = $this->redis->sort($key, array(
    'by' => 'nosort',
    'limit' => array($offset, $limit),
    'get' => array('post:*->created', 'post:*->author', 'post:*->message')
));
$key = 'posts';
$data = $this->redis->sort($key, array(
    'by' => 'nosort',
    'limit' => array(0, 10),
    'get' => 'post:*->data '
));
现在我的散列有X个成员,我想知道什么是最好的解决方案

是:

$key = 'posts';
$data = $this->redis->sort($key, array(
    'by' => 'nosort',
    'limit' => array($offset, $limit),
    'get' => 'post:*->data '
));
或者可能:

$key = 'posts';
$data = $this->redis->sort($key, array(
    'by' => 'nosort',
    'limit' => array($offset, $limit),
    'get' => '#'
));

foreach($data as $post_id) {
   $posts[] = $this->redis->hGetAll('post:'.$post_id);
}
或者最后:

$key = 'posts';
$data = $this->redis->sort($key, array(
    'by' => 'nosort',
    'limit' => array($offset, $limit),
    'get' => '#'
));

$pipeline = $this->redis->multi();
foreach ($data as $post_id) {
    $pipeline->hGetAll('post:'.$post_id);
}

return $pipeline->exec();
或者其他我还不知道的事情?
什么是最好、更快的方法?

如果从上的redis.io文档开始,您会发现有一些命令允许您获取多个哈希成员。特别是“”用于提取所有字段和值,或“”用于提取一组字段及其值


此外,对于设置它们,我建议使用“

一次性设置它们。如果您阅读过redis的源代码,您会发现这是不可能的。有一种解决方法,使用lua脚本在单个redis调用中组合“sort”和“hgetall”命令

“get模式”由函数“lookupKeyByPattern”处理。

我想从集合而不是散列中进行操作。我知道hashgetall,但这不是我想要的,每个hash键的“X成员”是常量吗?只有'id','created','author'?是的,但我有3个以上的字段,我有10个,你可以在sort命令中写10个'get pattern',没有限制。这是最快的方法。你有没有发现哪种方法效果最好?