【问题标题】:How to get redis key value with class function and init file如何使用类函数和init文件获取redis键值
【发布时间】:2018-06-11 18:07:34
【问题描述】:

在我的项目中,我使用redis。

我有一个init文件,包括ip端口和端口,所以Datasource类用于分析init文件和连接redis。

这是类 Datasource.php 代码,其中包含函数 getRedis():

namespace common;

class Datasource {

public function __construct() {}

public static function getRedis($config_name = NULL, $server_region = 'default') {

    global $config;
    $redis_config = $config['redis'][$config_name];

    if ($config_name && $redis_config && $server_region) {
        $this->_config_name = $config_name;
        $this->_redis_config = $redis_config;
        $this->_server_region = $server_region;

        try {
            $this->_redis = new \Redis();
            $this->_redis->connect($this->_redis_config[$server_region]['host'], $this->_redis_config[$server_region]['port']);
            if($this->_redis_config[$server_region]['password'] && !$this->_redis->auth($this->_redis_config[$server_region]['password'])) {
                $this->_redis = null;
            }
        } catch (Exception $e) {
            $this->_redis = null;
        }
    } else {
        $this->_redis = null;
    }

    return self::$this->_redis;
   }
 }// end of class Datasource

这里是redis.ini.php的init文件代码

<?php
 $config['redis']['instance1'] = array(
'default' => array(
    'host' => '127.0.0.1',
    'port' => '6379',
    'timeout' => 5,
    'pconnect' => 1,
    'password' => '',
  )
);
 $config['redis']['instance2'] = array(
'default' => array(
    'host' => '127.0.0.1',
    'port' => '6379',
    'timeout' => 5,
    'pconnect' => 1,
    'password' => '',
    )
  );

现在我想获取 redis 中的 xie 值,这是我的 html 代码:

<body style="height:100%" >
<?php 
include "o1ws1v/class/common/Datasource.php";
include 'o1ws1v/conf/redis.ini.php';
$redis_obj = common\Datasource::getRedis('instance1');

$value = $redis_obj->get("xie");
echo "get key xie is:".$value."\n";

?>
</body>

其实key xie应该是zuo。正确的结果是一行:“get key xie is:zuo”

但它什么也没显示,谁能帮帮我?

【问题讨论】:

  • xie 值在哪里设置为zuo
  • 在我的redis服务器中,我定义了xie值。在客户端,我可以成功获取 xie 并获取 zuo 返回值
  • var_dump($redis_obj)。是NULL吗?
  • @HtmHell,我用了“echo var_dump($redis_obj);”,没什么
  • 无回声。就在var_dump($redis_obj); 之后$redis_obj = common\Datas...

标签: php redis


【解决方案1】:

您在静态方法中使用$this,但您不能。另外,您在连接到 Redis 时会捕获所有异常,因此您不知道为什么它没有成功。你需要做两件事:

  1. 开启 PHP 错误(当然仅用于开发)
  2. 不要在连接时捕捉异常,如果你捕捉到了 - 然后记录/保留异常的消息。

试试这样的:

<?php

namespace common;

class Datasource
{
    private static $_redis, $_config_name, $_redis_config, $_server_region;

    public static function getRedis($config_name = NULL, $server_region = 'default')
    {
        error_reporting(E_ALL);
        ini_set("display_errors", true);

        global $config;

        $redis_config = $config['redis'][$config_name];

        if (!$config_name || !$redis_config || !$server_region) {
            throw new \Exception('$config_name or $redis_config or $server_region is not set');
        }

        if (!$redis_config[$server_region]['password']) {
            throw new \Exception('Redis password is not set');
        }

        self::$_config_name = $config_name;
        self::$_redis_config = $redis_config;
        self::$_server_region = $server_region;

        self::$_redis = new \Redis();

        self::$_redis->connect(self::$_redis_config[$server_region]['host'], self::$_redis_config[$server_region]['port']);

        if (!self::$_redis->auth(self::$_redis_config[$server_region]['password'])) {
            throw new \Exception("Can't login to Redis. Check password");
        }

        return self::$_redis;
    }
}

而且错误显示代码显然不属于这里,只是暂时看看有没有错误。

另外,我会添加一个条件来查看是否已设置 Redis,然后返回连接。否则每次调用getRedis 方法时都会建立另一个连接。像这样的:

public static function getRedis(...)
{
    if (!self::$_redis) {
        ...
    }

    return self::$_redis;
}

【讨论】:

    猜你喜欢
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    相关资源
    最近更新 更多