doubingguan3425 2018-06-29 15:04
浏览 137

将具有多个值的多个键设置为Redis

I have a php backend using phpredis (a php client for the redis server) to store key value pairs to a Redis server. The data I need to store is of this form:

"key1" => "v1", "v2", "v3"
"key2" => "m1", "m2", "m3"
"key3" => "n1", "n2", "n3"
...

Based on my research, I can set multiple keys in a redis using the mset command like so:

$redis->mSet(array('key0' => 'value0', 'key1' => 'value1'));

But what I actually need is something like this:

$redis->mSet(array('key0' => array('v1','v2','v3') , 'key1' => array('m1', 'm2', 'm3')));

But this just stores the value for each key as "Array" instead of the actual array specified.

Is this possible to do with a single command like mset or do I need to iterate my data and set each key separately using something like lPush?

phpredis documentation: https://github.com/phpredis/phpredis

  • 写回答

1条回答 默认 最新

  • dragonsun00000 2018-06-29 15:26
    关注

    So rather than using mSet you can probably use sADD to get your desired functionality.

    $redis->sAdd($key, ...$data);
    

    Full documentation on it here.

    This would mean iterating and doing it in multiple steps for which I'd reccomend reading into Redis Pipelines and the non-shameless plug link which contains more information.

    Which would look something like;

    $redis    = new Redis();
    $pipeline = $redis->multi(Redis::PIPELINE);
    
    foreach ($dataset as $data) {
        $pipeline->sAdd($data['key'], ...$data['values']);
    }
    
    $pipeline->exec();
    

    I can't think off the top of my head a way to do this in a singular operation, someone else might come along though who knows more than me :)


    Edit: Looks like I misunderstood your question a little as it was more focused on doing this in a single operation. Hopefully the above is still useful but to my knowledge you'll have to do this with multiple.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配