В моем проекте я использую redis.
И у меня есть файл инициализации, включая ip-порт и порт, поэтому класс Datasource используется для анализа файла инициализации и подключения 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
<?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' => '',
)
);
Теперь я хочу получить значение xie, котороенаходится в Redis, вот мой 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>
На самом деле, ключ xie должен быть ZUO.Следующим результатом является строка: «получить ключ xie is: zuo»
Но это ничего не показало, Кто мне может помочь?