Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Redis如何存储关联数组?集合、散列或列表?_Redis_Associative Array_Phpredis - Fatal编程技术网

Redis如何存储关联数组?集合、散列或列表?

Redis如何存储关联数组?集合、散列或列表?,redis,associative-array,phpredis,Redis,Associative Array,Phpredis,我对Redis的所有可用存储选项都有点困惑。 我想做一些简单的事情,我不想过度设计它。 我正在使用phpredis和redisv2.8.6 我需要存储这个简单的关联数组。我还需要能够通过它的键检索一个项目,并在所有项目上循环 $a = array( '12345' => array( 'name' => 'Post A', 'val2' => 'blah blah', 'val3' => 'blah blah bla

我对Redis的所有可用存储选项都有点困惑。 我想做一些简单的事情,我不想过度设计它。 我正在使用
phpredis
redisv2.8.6

我需要存储这个简单的关联数组。我还需要能够通过它的键检索一个项目,并在所有项目上循环

$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
type。像这样存储我的阵列:

foreach ($a as $key => $value) {
    $this->redis->hSet('posts', $key, json_encode($value));
}
public function getPost($postId)
{
    return json_decode($this->redis->hGet('posts', $postId), true);
}

// This is returning the information of Post A
$post = getPost(12345);
这样我就可以像这样轻松地访问密钥:

foreach ($a as $key => $value) {
    $this->redis->hSet('posts', $key, json_encode($value));
}
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\u id
存储在另一个列表中,以便能够循环所有帖子

因此,我的问题是,我应该使用哪种数据类型来存储我的帖子列表,允许我通过其id获取单个帖子并在所有帖子上循环

谢谢,
Maxime

您可以组合使用SET、Hash和SORT

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"

如果您不想使用sort,您可以使用SMEMBERS从集合中获取所有键名,然后使用Redis管道获取所有键

,仅针对寻找PHP代码的用户,以下是我最终使用的:

// Create a post hash
$key = 'post:'.$post->getId();
$this->redis->hSet($key, 'data', serialize($post->toArray()));

// Add a post in the account posts SET
$this->redis->sAdd($account->getId().':posts', $post->getId());

// You can execute the above code as many time as you need 
// to add an object in a SET

// Fetch the first $limit posts for this account
// SORT <account_id>:posts BY nosort GET <account_id>:post:*->data
$key = $account->getId().':posts';
$keys = $this->redis->sort($key, array(
    'by' => 'nosort',
    'limit' => array($offset, $limit),
    'get' => 'post:*->data'
));

// All Good !
var_dump($keys);
//创建一个post散列
$key='post:'。$post->getId();
$this->redis->hSet($key,'data',serialize($post->toArray());
//在帐户帖子集中添加帖子
$this->redis->sAdd($account->getId()。:posts',$post->getId());
//您可以根据需要多次执行上述代码
//在集合中添加对象的步骤
//获取此帐户的前$limit POST
//按nosort排序:post获取:post:->data
$key=$account->getId()':posts';
$keys=$this->redis->sort($key,数组)(
'by'=>'nosort',
“limit”=>数组($offset,$limit),
'获取'=>'发布:->数据'
));
//一切都好!
var_dump($keys);
我希望这将对你们中的一些人有所帮助;)

在PHP中,您只需

$redis->set($key, json_encode($value));
然后


或者使用您喜欢的任何序列化技术。JSON编码/解码的性能足以让我不在乎。

因此,除了将
post
的散列存储在
集中之外,没有其他解决方案了?如果我在
集合中有数百万个条目
,最好的选项是什么
排序
成员
?如果您注意到排序,我使用的是nosort,因此实际上它不会排序。另外,我认为理想的结构是散列来保持键值对,我确信你不会一次获取数百万个值,你只会获取很少的值,比如说50个,然后你可以使用排序限制。如果我想删除此集合中的所有哈希,该怎么办?我是否必须循环所有这些文件才能删除它们?你的意思是要清空集合还是还要删除散列?两者都是。我想删除
集合
及其包含的所有
散列
。我只是在做计算时使用Redis作为缓冲区,在计算后我将结果保存在MySQL中,我想删除不再使用的
Redis
存储对象。但是当你需要对数千个结果执行此操作时,可能会使用比需要更多的资源。使用仅限redis的解决方案将不太可能让您陷入性能问题。@BobKruithof是的,绝对是他妈的。这个答案有点肮脏,我有点为此感到羞耻。不过,对于我当时所做的工作,它确实工作得很好。您应该检查igbinary:
$igbinary=function\u exists('igbinary\u serialize')$编码=$igbinary?igbinary_serialize($data):serialize($data)它应该比常规序列化稍微快一点。