Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis - Check if key exists using predis

Tags:

php

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}?

like image 802
BugHunterUK Avatar asked Feb 04 '16 11:02

BugHunterUK


1 Answers

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.

like image 130
Eli Avatar answered Sep 28 '22 06:09

Eli