dqysi86208 2015-11-16 12:08
浏览 102
已采纳

在php循环中将数据写入redis时发生了有趣的事情

i wrote a php script to pull data from one server (lets call it Server A) to the other (Server B). data in server A is a redis list stores all the operating commands need to be written in server B, such as :

["setex",["session:xxxx",604800,"xxxx"]]
["set",["uid:xxx","xxxxx"]]
["pipeline",[]]
["set",["uid:xxx","xxxxx"]]
["hIncrBy",["Signin:xxxx","totalTimes",1]]
["pipeline",[]]
....

my php codes are :

while($i < 1000){
    $line = $redis['server_a']->rpop('sync:op');
    list($op,$params) = json_decode($line,1);
    $r = call_user_func_array(array($redis['server_b'], $op), $params);

    $i++;
}

The wired thing is, when the call_user_func_array method executes the redis command uncorrectly, all the rest commands in the queue cannot be written correctly into server B.

i stuck in this problem almost one week for seeking answers. after thousands of tests i found if i remove the "bad commands" that cannot be executed correctly, such as the ["pipeline",[]] row. all the other commands can be inserted properly. so it reminds me of some redis transaction issue. maybe there are some machanisms that when a command executed unproperly in redis , all the other commands afterwards will be treated as a transaction. so i add a exec() command into the while loop :

 while($i < 1000){
    $line = $redis['server_a']->rpop('sync:op');
    list($op,$params) = json_decode($line,1);
    $r = call_user_func_array(array($redis['server_b'], $op), $params);
    $redis['server_b']->exec(); //this is the significant update
    $i++;
}

then, my problem solved !!!

My question is , can anybody help me explain the redis machanism? is my assumption correct ?

  • 写回答

1条回答 默认 最新

  • duanguan3863 2015-11-16 12:51
    关注

    Your library is probably using transactions for pipelining for whatever reason. pipeline is no actual Redis command, see http://redis.io/commands

    Just strip all pipeline commands with empty arguments or just use ->exec when you issued a pipeline before.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵