什么是在PHP中使用Redis获取哈希列表的最佳方法

时间:2014-01-30 16:36:02

标签: php redis pipeline

我正在使用Redis和predis构建通知系统(不是移动推送通知,只是网站上的视觉警告)。 例如,当user1向user2发送消息时,我在Redis中创建一个条目。 在这种类型的事件中,我创建了一个哈希,其中包含有关通知的所有信息(日期,内容,发件人......)。 然后我将哈希键添加到特定于用户的列表中。

//creating the notification
notificationId = uniqid();
$this->redis->hmset($notificationId, array(
    "sender" => "sender's Name",
    "type" => "message",
    "user_id" => "recipient's id",
    "content" => "message content",
    "date" => new \Datetime()
    )
);
//adding the id of the notification in the user's message notifications list
$this->redis->lpush("messageList".$userId, $notificationId);

然后,当我想要为用户检索所有消息通知时:

$listName = "messageList:".$userId;
$arrNotifications = $this->redis->pipeline(function ($pipe) use ($listName) {

    foreach ($pipe->getClient()->lrange($listName, 0, -1) as $key => $id) {
        $arrNotif[] = $pipe->hgetall($id);
    }

});

我使用此方法获得所有期望的结果,但如果消息列表包含几千个条目,则操作需要0.5秒。它看起来有点慢,Redis以超快速而闻名。 所以我想知道我是否正确地做事。

有什么建议吗?

由于

1 个答案:

答案 0 :(得分:0)

如果您使用phpredis,则可能会对性能更满意。

这是你必须在服务器中编译的php C扩展。

我也开始使用predis,很高兴能够为phpredis提供更好的解决方案。

相关问题