本文整理汇总了PHP中Redis::auth方法的典型用法代码示例。如果您正苦于以下问题:PHP Redis::auth方法的具体用法?PHP Redis::auth怎么用?PHP Redis::auth使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Redis
的用法示例。
在下文中一共展示了Redis::auth方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: open
/**
* Establishes a DB connection.
* It does nothing if a DB connection has already been established.
* @return \Redis
* @throws Exception if connection fails
*/
public function open()
{
if ($this->_socket !== null) {
return;
}
$connection = ($this->unixSocket ?: $this->hostname . ':' . $this->port) . ', database=' . $this->database;
\Yii::trace('Opening redis DB connection: ' . $connection, __METHOD__);
$this->_socket = new \Redis();
if ($this->unixSocket) {
if ($this->persist) {
$this->_socket->pconnect($this->unixSocket, $this->port, $this->dataTimeout);
} else {
$this->_socket->connect($this->unixSocket, $this->port, $this->dataTimeout);
}
} else {
if ($this->persist) {
$this->_socket->pconnect($this->hostname, $this->port, $this->dataTimeout);
} else {
$this->_socket->connect($this->hostname, $this->port, $this->dataTimeout);
}
}
if (isset($this->password)) {
if ($this->_socket->auth($this->password) === false) {
throw new Exception('Redis authentication failed!');
}
}
$this->_socket->select($this->database);
return $this->_socket;
}
示例2: getConnection
/**
* Return redis connection object
*
* @return mixed Redis connection object on success, void or boolean on failure
*
* @since 3.4
*
* @throws RuntimeException
*/
protected function getConnection()
{
if (static::isSupported() == false) {
return false;
}
$config = JFactory::getConfig();
$app = JFactory::getApplication();
$caching = (bool) $config->get('caching');
if ($caching == false) {
return false;
}
$this->_persistent = $config->get('redis_persist', true);
$server = array('host' => $config->get('redis_server_host', 'localhost'), 'port' => $config->get('redis_server_port', 6379), 'auth' => $config->get('redis_server_auth', null), 'db' => (int) $config->get('redis_server_db', null));
static::$_redis = new Redis();
if ($this->_persistent) {
try {
$connection = static::$_redis->pconnect($server['host'], $server['port']);
$auth = !empty($server['auth']) ? static::$_redis->auth($server['auth']) : true;
} catch (Exception $e) {
}
} else {
try {
$connection = static::$_redis->connect($server['host'], $server['port']);
$auth = !empty($server['auth']) ? static::$_redis->auth($server['auth']) : true;
} catch (Exception $e) {
}
}
if ($connection == false) {
static::$_redis = null;
if ($app->isAdmin()) {
JError::raiseWarning(500, 'Redis connection failed');
}
return;
}
if ($auth == false) {
if ($app->isAdmin()) {
JError::raiseWarning(500, 'Redis authentication failed');
}
return;
}
$select = static::$_redis->select($server['db']);
if ($select == false) {
static::$_redis = null;
if ($app->isAdmin()) {
JError::raiseWarning(500, 'Redis failed to select database');
}
return;
}
try {
static::$_redis->ping();
} catch (RedisException $e) {
static::$_redis = null;
if ($app->isAdmin()) {
JError::raiseWarning(500, 'Redis ping failed');
}
return;
}
return static::$_redis;
}
示例3: __construct
/**
* @param $host
* @param $port
* @param null $auth
* @param int $timeout
*/
public function __construct($host, $port, $auth = null, $timeout = 2)
{
$this->redis = new \Redis();
$this->redis->connect($host, $port, $timeout);
if (null !== $auth) {
$this->redis->auth($auth);
}
}
示例4: connect
/**
* Create the Redis connector.
*
* We have a $use_redis parameter here so that we can allow devs the ability of using
* a development environment without using Redis. Passing false will mimic the data
* coming and going from Redis.
*
* @param boolean $use_redis
* @param string $redis_hostname
* @param string $redis_port
* @param string $redis_password
* @return $this
*/
public function connect($use_redis, $redis_hostname, $redis_port, $redis_password)
{
if ($use_redis === true) {
$this->redis = new \Redis();
$this->redis->connect($redis_hostname, $redis_port);
$this->redis->auth($redis_password);
}
return $this;
}
示例5: setConnection
/**
* 设置连接
*/
private function setConnection()
{
$this->connection = new \Redis();
$this->connection->connect($this->config_object->host(), $this->config_object->port());
$auth = $this->config_object->auth();
if (!empty($auth)) {
$this->connection->auth($auth);
}
$database = $this->config_object->database();
if ($database != 0) {
$this->connection->select($database);
}
}
示例6: getClient
/**
* Gets the redis client
* @return Redis the redis client
*/
public function getClient()
{
if ($this->_client === null) {
$this->_client = new Redis();
$ret = $this->_client->connect($this->hostname, $this->port);
if (isset($this->password)) {
if ($this->_client->auth($this->password) === false) {
throw new CException('Redis authentication failed!');
}
}
$this->_client->select($this->database);
}
return $this->_client;
}
示例7: connection
/**
* 链接到客户端
* @param array $options 链接配置信息
* @return void
*/
public function connection()
{
$client = $clientWrite = null;
switch ($this->type) {
case null:
$client = $this->createConnection($this->getHost("read"));
$clientWrite = $this->createConnection($this->getHost("write"));
case "read":
$client = $this->createConnection($this->getHost("read"));
case "write":
$client = $this->createConnection($this->getHost("write"));
break;
}
if ($client === false || $clientWrite === false) {
return $this->statu = false;
}
$this->client = $client;
$this->clientWrite = $clientWrite;
if (isset($this->client)) {
$this->client->auth($this->options["password"]);
$this->client->select($this->options["database"]);
}
if (isset($this->clientWrite)) {
$this->clientWrite->auth($this->options["password"]);
$this->clientWrite->select($this->options["database"]);
} else {
$this->clientWrite = $this->client;
}
return $this->statu = true;
}
示例8: connRedis
public static function connRedis($instance)
{
$host = 'xx.m.cnhza.kvstore.aliyuncs.com';
$port = '6379';
$user = 'xxx';
$pwd = 'xxx';
$redisObj = new Redis();
$tmp_count = 0;
$conn_sec = FALSE;
while ($tmp_count < 3 and $conn_sec === FALSE) {
$tmp_count = $tmp_count + 1;
try {
if ($redisObj->connect($host, $port, self::$CONN_TIMEOUT)) {
$conn_sec = TRUE;
$redisObj->auth($user . ":" . $pwd);
} else {
$conn_sec = FALSE;
}
} catch (RedisException $e) {
$conn_sec = FALSE;
}
}
if ($redisObj instanceof Redis and isset($redisObj->socket)) {
return $redisObj;
}
return FALSE;
}
示例9: _connect
/**
* Connects to a Redis server
*
* @return bool True if Redis server was connected
*/
protected function _connect()
{
try {
$server = $this->_config['host'];
if (empty($server) && !empty($this->_config['server'])) {
$server = $this->_config['server'];
}
$this->_Redis = new \Redis();
if (!empty($this->settings['unix_socket'])) {
$return = $this->_Redis->connect($this->settings['unix_socket']);
} elseif (empty($this->_config['persistent'])) {
$return = $this->_Redis->connect($this->_config['server'], $this->_config['port'], $this->_config['timeout']);
} else {
$persistentId = $this->_config['port'] . $this->_config['timeout'] . $this->_config['database'];
$return = $this->_Redis->pconnect($this->_config['server'], $this->_config['port'], $this->_config['timeout'], $persistentId);
}
} catch (\RedisException $e) {
return false;
}
if ($return && $this->_config['password']) {
$return = $this->_Redis->auth($this->_config['password']);
}
if ($return) {
$return = $this->_Redis->select($this->_config['database']);
}
return $return;
}
示例10: __construct
/**
* Loads the configured driver and validates it.
* @param array|string|bool $config custom configuration or config group name
* @throws Kohana_Exception
*/
public function __construct($config = FALSE)
{
if (is_string($config)) {
$name = $config;
// Test the config group name
if (($config = Kohana::config('redis.' . $config)) === NULL) {
throw new Kohana_Exception('cache.undefined_group', $name);
}
}
if (is_array($config)) {
// Append the default configuration options
$config += Kohana::config('redis.default');
} else {
// Load the default group
$config = Kohana::config('redis.default');
}
// Cache the config in the object
$this->config = $config;
parent::connect($this->config['host'], $this->config['port'], $this->config['timeout']);
if (!empty($config['auth'])) {
parent::auth($config['auth']);
}
if (!empty($config['db'])) {
parent::select($config['db']);
}
Kohana::log('debug', 'Redis Library initialized');
}
示例11: __construct
public function __construct($settings)
{
parent::bootstrap();
$t = microtime(true);
$redis = new \Redis();
try {
$redis->connect($settings['host'], $settings['port']);
if (isset($settings['password']) && !empty($settings['password'])) {
if ($redis->auth($settings['password']) === false) {
throw new \Exception('Unable to authenticate with redis!');
}
}
if (isset($settings['database']) && !empty($settings['database'])) {
if ($redis->select($settings['database']) === false) {
throw new \Exception('Unable to Redis database select');
}
}
} catch (\Exception $e) {
throw new \Exception('Unable to connect to Redis server');
}
if (isset($settings['prefix'])) {
$redis->setOption(\Redis::OPT_PREFIX, $settings['prefix'] . ':');
}
parent::$serviceInstance[get_class()] = $redis;
$queryTime = round((microtime(true) - $t) * 1000, 2);
self::logQuery(array('command' => 'CONNECTION to ' . $settings['host'] . ':' . $settings['port'], 'time' => $queryTime));
self::$_totalTime[get_class()] += $queryTime;
self::$_totalQueries[get_class()]++;
}
示例12: _connect
/**
* Connects to a Redis server
*
* @return bool True if Redis server was connected
*/
protected function _connect()
{
try {
$hash = "{$this->settings['server']}:{$this->settings['port']}:{$this->settings['database']}";
if (!empty(self::$instances[$hash])) {
$return = $this->_Redis = self::$instances[$hash];
} else {
$this->_Redis = new Redis();
self::$instances[$hash] = $this->_Redis;
if (!empty($this->settings['unix_socket'])) {
$return = $this->_Redis->connect($this->settings['unix_socket']);
} elseif (empty($this->settings['persistent'])) {
$return = $this->_Redis->connect($this->settings['server'], $this->settings['port'], $this->settings['timeout']);
} else {
$persistentId = $this->settings['port'] . $this->settings['timeout'] . $this->settings['database'];
$return = $this->_Redis->pconnect($this->settings['server'], $this->settings['port'], $this->settings['timeout'], $persistentId);
}
}
} catch (RedisException $e) {
$return = false;
}
if (!$return) {
return false;
}
if ($this->settings['password'] && !$this->_Redis->auth($this->settings['password'])) {
return false;
}
return $this->_Redis->select($this->settings['database']);
}
示例13: init
/**
* 获取实例
*
* @return void
* @author seatle <seatle@foxmail.com>
* @created time :2016-04-10 22:55
*/
public static function init()
{
if (extension_loaded('Redis')) {
$_instance = new Redis();
} else {
$errmsg = "extension redis is not installed";
log::add($errmsg, "Error");
return null;
}
// 这里不能用pconnect,会报错:Uncaught exception 'RedisException' with message 'read error on connection'
$_instance->connect($GLOBALS['config']['redis']['host'], $GLOBALS['config']['redis']['port'], $GLOBALS['config']['redis']['timeout']);
// 验证
if ($GLOBALS['config']['redis']['pass']) {
if (!$_instance->auth($GLOBALS['config']['redis']['pass'])) {
$errmsg = "Redis Server authentication failed!!";
log::add($errmsg, "Error");
return null;
}
}
// 不序列化的话不能存数组,用php的序列化方式其他语言又不能读取,所以这里自己用json序列化了,性能还比php的序列化好1.4倍
//$_instance->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // don't serialize data
//$_instance->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); // use built-in serialize/unserialize
//$_instance->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // use igBinary serialize/unserialize
$_instance->setOption(Redis::OPT_PREFIX, $GLOBALS['config']['redis']['prefix'] . ":");
return $_instance;
}
示例14: __construct
/**
* Class constructor
*
* Setup Redis
*
* Loads Redis config file if present. Will halt execution
* if a Redis connection can't be established.
*
* @return void
* @see Redis::connect()
*/
public function __construct()
{
$config = array();
$CI =& get_instance();
if ($CI->config->load('redis', TRUE, TRUE)) {
$config = $CI->config->item('redis');
}
$config = array_merge(self::$_default_config, $config);
$this->_redis = new Redis();
try {
if ($config['socket_type'] === 'unix') {
$success = $this->_redis->connect($config['socket']);
} else {
$success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']);
}
if (!$success) {
log_message('error', 'Cache: Redis connection failed. Check your configuration.');
}
if (isset($config['password']) && !$this->_redis->auth($config['password'])) {
log_message('error', 'Cache: Redis authentication failed.');
}
} catch (RedisException $e) {
log_message('error', 'Cache: Redis connection refused (' . $e->getMessage() . ')');
}
// Initialize the index of serialized values.
$serialized = $this->_redis->sMembers('_ci_redis_serialized');
empty($serialized) or $this->_serialized = array_flip($serialized);
}
示例15: _setup_redis
/**
* Setup Redis config and connection
*
* Loads Redis config file if present. Will halt execution
* if a Redis connection can't be established.
*
* @return bool
* @see Redis::connect()
*/
protected function _setup_redis()
{
$config = array('host' => '127.0.0.1', 'password' => NULL, 'port' => 6379, 'timeout' => 0);
if (($user_config = ee()->config->item('redis')) !== FALSE) {
$config = array_merge($config, $user_config);
}
$this->_redis = new Redis();
// Our return value which we will update as we setup Redis; if it's
// TRUE at the end, allow Redis to be used
$result = FALSE;
try {
$result = $this->_redis->connect($config['host'], $config['port'], $config['timeout']);
} catch (RedisException $e) {
log_message('debug', 'Redis connection refused: ' . $e->getMessage());
$this->_redis = FALSE;
return FALSE;
}
// Redis will return FALSE sometimes instead of throwing an exeption
if (!$result) {
log_message('debug', 'Redis connection failed.');
$this->_redis = FALSE;
return FALSE;
}
// If a password is set, attempt to authenticate
if (!empty($config['password']) && $result) {
$result = $this->_redis->auth($config['password']);
}
return $result;
}