php - 如何在 php 中获取 redis->lpush() 的返回值?

标签 php redis

我想知道我可以在 php 中获取 $redis->lpush 的返回值吗?我试图打印出返回值,但是好像什么都没有,然后我在网上搜索了一下,有些文档说这个方法会返回列表的长度,这让我很困惑。

         $id = $this->post('id');
         $info = $this->PushModel->getPush($id);

         if(empty($info)){
             $this->json(Constant::FAILURE);
         }

         $gameId = $info['game_id'];
         $title = $info['title'];
         $content = $info['content'];
         $pushInfo = "$id|$gameId|$title|$content";

         $redis = $this->redis();
         $ret = $redis->connect(REDIS_HOST, ZGH_REDIS_PORT);

         $res = $redis->lPush(REDIS_HRGAME_PUSH_BATCH_KEY,$pushInfo);

         print_r($ret);
         print_r($res);exit;

最佳答案

不确定你的意思 - 这很好用:

#!/usr/local/bin/php -f
<?php
   $redis = new Redis();
   $redis->connect('127.0.0.1',6379);
   $key = "MyFunkyKey";

   # Start with a clean slate
   $res = $redis->del($key);
   var_dump($res);

   # LPUSH key twice
   $res = $redis->lPush($key,1);
   var_dump($res);
   $res = $redis->lPush($key,1);
   var_dump($res);
?>

示例输出

int(1)
int(1)
int(2)

这是我的版本信息:

$ pecl info redis

About pecl.php.net/redis-4.0.2
==============================
Release Type          PECL-style PHP extension (source code)
Name                  redis
Channel               pecl.php.net
Summary               PHP extension for interfacing with Redis
Description           This extension provides an API for communicating
                      with Redis servers.
Maintainers           Nicolas Favre-Felix <n.favrefelix@gmail.com>
                      (lead)
                      Michael Grunder <michael.grunder@gmail.com>
                      (lead)
                      Pavlo Yatsukhnenko <p.yatsukhnenko@gmail.com>
                      (lead)
Release Date          2018-04-25 11:28:55
Release Version       4.0.2 (stable)
API Version           4.0.2 (stable)
License               PHP (http://www.php.net/license)
Release Notes         phpredis 4.0.2

                      This release contains only fix of exists method
                      to take multiple keys
                      and return integer value (was broken in 4.0.1)
                      Thanks @RanjanRohit!
Required Dependencies PHP version 5.3.0-7.9.99
                      PEAR installer version 1.4.0b1 or newer
package.xml version   2.0
Last Modified         2018-05-31 06:54
Previous Installed    - None -
Version

关于php - 如何在 php 中获取 redis->lpush() 的返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50615552/

相关文章:

python - REDIS中key过期如何回调

php - 根据选择值显示/隐藏字段

javascript - 如何将 JavaScript 响应添加到多个 forearch 结果

data-structures - 在redis中驱逐 HashMap 中的键

list - 如何加入redis中的2个列表

ruby-on-rails - Redis 数据在应用程序重启时重置为默认值

django - 如何在 celery 内为每个用户生成队列?

php - 数组中未选中的复选框

php - 如何从 PHP 中的日期时间中删除 TIME END

javascript - 从 JavaScript 传递并获取 ID 到 PHP Controller