Reputation: 299
my dev environment is the Laravel PHP framework utilising the predis client library.
I am probably overlooking certain aspects, and I am new to Redis however what I think am wanting is the hash data type for storing user information like so:
$redis->hmset('users', array('id' : 1, 'username' => 'ali', 'dob' => '29/06/89', 'email' => '[email protected]'));
My question is however with the above is this the correct way of me storing all users with there information in ONE hash so I can retrieve all users together and retrieve an individual user by ID when necessary, as I am in need of displaying all users in a table on the front end, but also need to grab an individual user by there ID on occasion.
If not and I need to store each user in an individual hash like below with there ID as part of the key how do I retrieve all users in one go and ordered?
$redis->hmset('users:{id}', array('name' => 'ali', 'dob' => '29/06/89', 'email' => '[email protected]'));
Many thanks for any help.
Upvotes: 0
Views: 1174
Reputation: 739
What I've done is as you've said, give each users an id and store them in the format users:{id}
.
Then you'll want to to store all the user id's in a set, or perhaps a sorted set or list if order matters to you, so something like
$redis->sadd('users', {user's id});
This is psuedo code for storing in a set because I am not sure of the exact formatting of predis.
Then when you want to retrieve all users, you first do $redis->smembers('users')
to get all the user ids, and then iterate over those id's to retrieve all the user hashes.
Upvotes: 1