Redis如何存储关联数组?设置还是散列还是列表? [英] Redis how to store associative array? Set or Hash or List?

查看:179
本文介绍了Redis如何存储关联数组?设置还是散列还是列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Redis的所有可用存储选项感到有些困惑. 我想做一些简单的事情,而又不想过度设计. 我正在使用phpredisRedis v2.8.6.

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',
    )
);

所以到目前为止,我一直在使用hash类型.像这样存储我的数组:

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);

但是现在我需要遍历所有帖子,我不知道该怎么做以及是否可以使用当前的结构来完成.我不知道是否需要将所有post_id存储在另一个列表中以便能够遍历所有帖子?

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?

所以我的问题是我应该使用哪种数据类型来存储我的帖子列表,从而允许我通过ID来获取单个帖子并遍历所有帖子?

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?

谢谢, 马克西姆

推荐答案

您可以将SET以及Hash和SORT结合使用

You can use SET and Hash and SORT in combination

redis 127.0.0.1:6379> HMSET TEST_12345 name "Post A" val2 "Blah Blah" val3 "Blah Blah Blah"
OK
redis 127.0.0.1:6379> HMSET TEST_54321 name "Post B" val2 "Blah Blah" val3 "Blah Blah Blah"
OK
redis 127.0.0.1:6379> HMSET TEST_998877 name "Post C" val2 "Blah Blah" val3 "Blah Blah Blah"
OK
redis 127.0.0.1:6379> SADD All_keys TEST_12345 TEST_54321 TEST_998877
(integer) 3
redis 127.0.0.1:6379> HGETALL TEST_12345

要获取一个哈希值:

redis 127.0.0.1:6379> HGETALL TEST_12345
1) "name"
2) "Post A"
3) "val2"
4) "Blah Blah"
5) "val3"
6) "Blah Blah Blah"

获取所有哈希值

redis 127.0.0.1:6379> SORT All_keys BY nosort GET *->name GET *->val2 GET *->val3
1) "Post A"
2) "Blah Blah"
3) "Blah Blah Blah"
4) "Post B"
5) "Blah Blah"
6) "Blah Blah Blah"
7) "Post C"
8) "Blah Blah"
9) "Blah Blah Blah"

如果您不想使用排序,则可以使用 SMEMBERS 从SET中获取所有键名,然后使用Redis Pipeline来获取所有键

If you don't want to use sort you can use Fetch All the key names from SET using SMEMBERS and then use Redis Pipeline to fetch all the keys

这篇关于Redis如何存储关联数组?设置还是散列还是列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆