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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧