【发布时间】: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...