I'm a bit confused with all the available storing options of Redis.
I want to do something simple and I don't want to over engineer it.
I'm working with phpredis
and Redis v2.8.6
.
I have this simple associative array that I need to store. I also need to be able to retrieve an item by its key and loop over all the items.
$a = array(
'12345' => array(
'name' => 'Post A',
'val2' => 'blah blah',
'val3' => 'blah blah blah',
),
'54321' => array(
'name' => 'Post B',
'val2' => 'blah blah',
'val3' => 'blah blah blah',
),
'998877' => array(
'name' => 'Post C',
'val2' => 'blah blah',
'val3' => 'blah blah blah',
)
);
So what I was doing till now was using hash
type. storing my array like this:
foreach ($a as $key => $value) {
$this->redis->hSet('posts', $key, json_encode($value));
}
Like that I could access the key easily like this:
public function getPost($postId)
{
return json_decode($this->redis->hGet('posts', $postId), true);
}
// This is returning the information of Post A
$post = getPost(12345);
But now I need to loop over all the posts I don't know how to do it and if I can do it with my current structure. I don't know if I need to store all the post_id
in another list to be able to loop over all the posts?
So my question is which data type(s) should I use to store my list of posts, allowing me to fetch a single post by its id and looping over all the posts?
Thanks, Maxime
post
in aSET
? What is the best optionsSORT
orSMEMBERS
if I have millions entries in theSET
? – Catenate