在我的项目中,我在Datasource.php文件中定义了两个类Datasource和RedisHelper。
Datasource用于从redis获取数据。
RedisHelper用于连接redis。
这是Datasource.php的代码:
<?php
namespace common;
class Datasource {
public static $redises = array();
public function __construct() {}
public static function getRedis($config_name = NULL, $server_region = 'default') {
global $config;
$redis_config = $config['redis'][$config_name];
try {
self::$redises[$config_name] = RedisHelper::instance($config_name, $redis_config, $server_region);
} catch (Exception $e) {
self::$redises[$config_name] = null;
}
return self::$redises[$config_name];
}
}
class RedisHelper {
private $_config_name = "";
private $_redis_config = null;
private $_server_region = null;
public $timeout = 1;
private $_redis = null;
private static $instances = array();
private static $connect_error = 0;
private $call_error = 0;
private function __construct($config_name, $redis_config, $server_region) {
if ($config_name && $redis_config && $server_region) {
$this->_config_name = $config_name;
$this->_redis_config = $redis_config;
$this->_server_region = $server_region;
$this->timeout = isset($this->_redis_config[$server_region]['timeout']) ? $this->_redis_config[$server_region]['timeout'] : $this->timeout;
try {
$this->_redis = new \redis();
$this->_redis->connect($this->_redis_config[$server_region]['host'], $this->_redis_config[$server_region]['port'], $this->timeout);
} catch (Exception $e) {
$this->_redis = null;
}
} else {
$this->_redis = null;
}
}
public static function instance($config_name, $redis_config, $server_region) {
if (!$config_name || !$redis_config) {
return false;
}
$only_key = $config_name . ':' . $server_region;
if (!isset(self::$instances[$only_key])) {
try {
self::$instances[$only_key] = new RedisHelper($config_name, $redis_config, $server_region);
self::$connect_error = 0;
} catch (Exception $e) {
if (self::$connect_error < 2) {
self::$connect_error += 1;
return RedisHelper::instance($config_name, $redis_config, $server_region);
} else {
self::$connect_error = 0;
self::$instances[$only_key] = new RedisHelper(false, false, false);
}
}
}
$redis_config_info = array();
if ($redis_config && isset($redis_config[$server_region]) && isset($redis_config[$server_region]['password'])) {
$redis_config_info = $redis_config[$server_region];
unset($redis_config_info['password']);
}
self::$connect_error = 0;
return self::$instances[$only_key];
}
}
这是redis.ini.php文件:
<?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与类连接,这是我的HTML代码:
<body style="height:100%" >
<div><h3>haha</h3></div>
<?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>
但它失败了。 $ redis_obj在我的控制台中什么都没有。谁能帮帮我?