duan5801 2016-02-04 11:38
浏览 48
已采纳

Redis - 使用predis检查密钥是否存在

Using predis is it possible to check if a key exists?

My users data is stored as follows:

public function createUser($email, $password, $username)
{
    return $this->predis->hMset("user:{$username}", [
        'email' => $email,
        'password' => $password,
        'username' => $username
    ]);
}

Now, when I check to see if a user exists I do the following:

public function checkUserExists($username)
{
    return $this->predis->hExists("user:{$username}", 'username');
}

Is it possible to check if the user exists without having to check if the key exists? For example by just checking user:{$username}?

  • 写回答

1条回答 默认 最新

  • dphw5101 2016-02-04 21:01
    关注

    Yes. Since your key is essentially just the user name, you can just see if the key exists. You can use Redis' EXISTS for this:

    public function checkUserExists($username)
    {
        return $this->predis->exists("user:{$username}");
    }
    

    The speed difference between the two will be very, very small, but using exists will make your code a bit cleaner.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图