search

Home  >  Q&A  >  body text

phpredis - Redis中incr的问题

Redis版本

我后台的程序设置了会员ID的起始数

<?php
$redis->set('userId',intval($start));
?>

前台的会员注册会获取这个userId,然后当做它的ID

<?php
$userId = $redis->get('userId');
if($model->insert(['userId'=>$userId])){
    $redis->incr('userId');
}

但现在的问题是,我无论怎么注册会员,这个redis中的userId都不会变,在服务器上操作

incr userId

会返回错误“(error) ERR value is not an integer or out of range”

get userId
"i:10003;"

求大神,没用过几次redis,有些蒙

阿神阿神2798 days ago752

reply all(3)I'll reply

  • 怪我咯

    怪我咯2017-04-24 09:12:30

    I guess you used it when initializing the code

    $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
    

    So the final value to the database is a string "i:10003;", and the string cannot be incr

    $r = $redis->incr('userId'); // 这里的返回值应该是 false
    

    If you must use serialization, you can only use zIncrBy. What is serialized by zIncrBy is member and the operation is score, so it will not be affected

    reply
    0
  • PHP中文网

    PHP中文网2017-04-24 09:12:30

    This is okay for me, you can try it according to my instructions.

    reply
    0
  • PHPz

    PHPz2017-04-24 09:12:30

    "i:10003;"The userId set is not a number at all

    reply
    0
  • Cancelreply