直接在Gateway.php写代码使用redis,报错

xamarin
<?php
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */
namespace GatewayWorker\Lib;

use Exception;
use GatewayWorker\Protocols\GatewayProtocol;
use Workerman\Connection\TcpConnection;

/**
 * 数据发送相关
 */
class Gateway
{

private $redis;

    public $checkUserReadable = false;

    public static function setChatRecord($from, $to, $message) {
        $data = array('from' => $from, 'to' => $to, 'message' => $message, 'sent' => time());
        $value = json_encode($data);
        //生成json字符串
        $keyName = 'rec:' . self::getRecKeyName($from, $to);
            $redis = new Redis(); 
            $redis->connect('127.0.0.1', 6379);   
        $res =  $redis -> lPush($keyName, $value);
        if (!$this -> checkUserReadable) {//消息接受者无法立刻查看时,将消息设置为未读
            return $redis -> hIncrBy('unread_' . $to, $from, 1);
        }
        return $res;

    }
       private static function getRecKeyName($from, $to) 
            { if ($from > $to){return  $to . '_' . $from;

                          }else{ return  $from . '_' . $to;}
            }
    /**
     * gateway 实例
     */

报错信息

Fatal error: Class 'GatewayWorker\Lib\Redis' not found in /pi/components/com_work/GatewayWorker/Lib/Gateway.php on line 49
WORKER EXIT UNEXPECTED E_ERROR Class 'GatewayWorker\Lib\Redis' not found in /pi/components/com_work/GatewayWorker/Lib/Gateway.php on line 49
worker exit with status 65280

49行就是$redis = new Redis(); 是不是不能直接使用redis.

6140 10 0
10个回答

latin

注意PHP的命名空间,应该是 new \Redis();

php.net/manual/zh/language.namespaces.php

  • 暂无评论
xamarin

改成 new \Redis();现在还有报错 WORKER EXIT UNEXPECTED E_ERROR Class 'Redis' not found in /pi/components/com_work/GatewayWorker/Lib/Gateway.php on line 31

  • 暂无评论
latin

没装redis扩展

  • 暂无评论
latin

。。。。

  • 暂无评论
xamarin

安装了。下面是# service redis-server status
● redis-server.service - SYSV: Redis is a persistent key-value database
Loaded: loaded (/etc/rc.d/init.d/redis-server)
Active: active (running) since Thu 2016-07-14 00:10:36 CST; 16h ago
Docs: man:systemd-sysv-generator(8)
Process: 2145 ExecStart=/etc/rc.d/init.d/redis-server start (code=exited, status=0/SUCCESS)
Main PID: 2164 (redis-server)
CGroup: /system.slice/redis-server.service
└─2164 /usr/local/redis/bin/redis-server 127.0.0.1:6379

Jul 14 00:10:36 localhost.localdomain systemd: Starting SYSV: Redis is a persistent key-value database...
Jul 14 00:10:36 localhost.localdomain redis-server: Starting redis-server:
Jul 14 00:10:36 localhost.localdomain systemd: PID file /var/run/redis.pid not readable (yet?) after start.
Jul 14 00:10:36 localhost.localdomain systemd: Started SYSV: Redis is a persistent key-value database.

  • 暂无评论
latin

reidis扩展,不是redis服务端。
类似mysql 扩展和 mysql 服务端的区别

  • 暂无评论
xamarin

现在问题在这个地方,在单独的php文件测试redis功能都很正常,在workerman里写就报错

  • 暂无评论
latin

workerman是php命令行运行的,你要保证php命令行装了redis扩展。

运行 php -m ,如果没看到redis,就是php命令行没装redis扩展。

手册上有扩展安装方法
http://doc3.workerman.net/appendices/install-extension.html

  • 暂无评论
xamarin

刚刚用php -m查看,确实没有redis .,现在在非命令行下已经有redis,不知道在命令行redis怎么安装

  • 暂无评论
xamarin

通过卸载重装redis,已经不报错了。谢谢。

  • 暂无评论
年代过于久远,无法发表回答
×
🔝