A
A
Alexander Sharomet2017-02-08 17:17:10
PHP
Alexander Sharomet, 2017-02-08 17:17:10

How to properly pull data in redis?

Hello.
who knows how to properly pull data into nosql redis.
So
I create a user key in it with a guid value that contains an array.

$this->hmset('users', Guid::getGuid(), 
  json_encode([
    'name' => 'username',
    'email' => 'useremail',
    'password' => password_hash('userpassword', PASSWORD_DEFAULT)
  ])
);

And how to get and compare - whether the user exists or not.
This method works, but is it correct to pull out all the data from the table in order to compare them? After all, there could be thousands of them.
$data = $this->hgetall('users');//Получаем все данные из ключа 'users'
foreach ($data as $key => $value) {
  if(json_decode($value, true)['name']=='username'){//Проверяем на совпадение
    print_r('Пользователь найден');
  }
}

Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
ThunderCat, 2017-02-08
@sharomet

So, it's a little unclear (in fact, a lot of it is unclear), what is going on?
As a result, a key (?) users is created (not user at all), judging by the code. Though it is necessary to guess that there in general occurs in function.
I don’t know something, or the key-value assumes that by the name of user I will get 1 value (in this case, a jsencode string?), who is the "guid value"? what does the stored record look like in the end? Or do you have many?
It's a good idea to stick to the schema when constructing keys: "object-type:id:field". With it, your selection, firstly, will be fast, and secondly, it will not require sampling and parsing of EACH user, it is enough to know the id or name, for example.

A
Alexander Sharomet, 2017-02-08
@sharomet

what does the stored record look like in the end?

6815eb83d4dc4f2da0521ec25cd69918.png

@
@mgyk, 2017-02-09
_

You chose not the best option data type (hash) in essence, you make only one key / value pair. But, radish makes it possible to partially update it. I think it will be much easier to use the string type and write the keys
set('users:guid', JSON), that is, the key will be the string users + ":" + Guid::getGuid(), and the value is your json
get('users:guid '), respectively, to read the value.
full list of commands here
https://redis.io/commands/#string
ps your hash

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question