/**
  * 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;
 }
Beispiel #2
0
 /**
  * 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;
 }
Beispiel #3
0
 /**
  * @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);
     }
 }
 /**
  * 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;
 }
Beispiel #5
0
 /**
  * 设置连接
  */
 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);
     }
 }
 /**
  * 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;
 }
Beispiel #7
0
 /**
  * 链接到客户端
  * @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;
 }
 public static function connRedis($instance)
 {
     $host = 'xx.m.cnhza.kvstore.aliyuncs.com';
     $port = '6379';
     $user = '******';
     $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;
 }
Beispiel #9
0
 /**
  * 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;
 }
Beispiel #10
0
 /**
  * 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');
 }
Beispiel #11
0
 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()]++;
 }
Beispiel #12
0
 /**
  * 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']);
 }
Beispiel #13
0
 /**
  * 获取实例
  * 
  * @return void
  * @author seatle <*****@*****.**> 
  * @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;
 }
Beispiel #14
0
 /**
  * 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);
 }
Beispiel #15
0
 /**
  * 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;
 }
Beispiel #16
0
 protected function cache()
 {
     $redis = new Redis();
     $redis->connect('127.0.0.1', 6379);
     $redis->auth('123456');
     return $redis;
 }
Beispiel #17
0
 public static final function openConnection($target)
 {
     if (empty(self::$redisInfo)) {
         self::parseConnectionInfo();
     }
     $connection = new Redis();
     if (isset(self::$redisInfo[$target])) {
         $info = self::$redisInfo[$target];
     } else {
         $info = array('host' => '127.0.0.1', 'port' => 6379, 'timeout' => 0, 'database' => 0, 'password' => '', 'options' => array());
     }
     try {
         $connection->connect($info['host'], $info['port'], $info['timeout']);
         if ($info['password']) {
             $connection->auth($info['password']);
         }
         $connection->select($info['database']);
         foreach ($info['options'] as $k => $v) {
             $connection->setOption($k, $v);
         }
     } catch (Exception $e) {
         $connection = null;
     }
     return $connection;
 }
 /**
  * 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();
     $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('debug', 'Cache: Redis connection refused. Check the config.');
             return FALSE;
         }
     } catch (RedisException $e) {
         log_message('debug', 'Cache: Redis connection refused (' . $e->getMessage() . ')');
         return FALSE;
     }
     if (isset($config['password'])) {
         $this->_redis->auth($config['password']);
     }
     return TRUE;
 }
Beispiel #19
0
 public function connect()
 {
     if ($this->handler) {
         return $this->handler;
     }
     $config = $this->config;
     $handler = new \Redis();
     // 优先使用unix socket
     $conn_args = $config['unix_socket'] ? array($config['unix_socket']) : array($config['host'], $config['port'], $config['timeout']);
     if ($this->isPersistent()) {
         $conn_args[] = $config['persistent_id'];
         $conn = call_user_func_array(array($handler, 'pconnect'), $conn_args);
     } else {
         $conn = call_user_func_array(array($handler, 'connect'), $conn_args);
     }
     if (!$conn) {
         throw new \Owl\Service\Exception('Cannot connect redis');
     }
     if ($config['password'] && !$handler->auth($config['password'])) {
         throw new \Owl\Service\Exception('Invalid redis password');
     }
     if ($config['database'] && !$handler->select($config['database'])) {
         throw new \Owl\Service\Exception('Select redis database[' . $config['database'] . '] failed');
     }
     if (isset($config['prefix'])) {
         $handler->setOption(\Redis::OPT_PREFIX, $config['prefix']);
     }
     return $this->handler = $handler;
 }
 /**
  * {@inheritdoc}
  */
 public function getAdapter(array $config)
 {
     $client = new \Redis();
     $dsn = $this->getDsn();
     if (empty($dsn)) {
         if (false === $client->connect($config['host'], $config['port'])) {
             throw new ConnectException(sprintf('Could not connect to Redis database on "%s:%s".', $config['host'], $config['port']));
         }
     } else {
         if (false === $client->connect($dsn->getFirstHost(), $dsn->getFirstPort())) {
             throw new ConnectException(sprintf('Could not connect to Redis database on "%s:%s".', $dsn->getFirstHost(), $dsn->getFirstPort()));
         }
         if (!empty($dsn->getPassword())) {
             if (false === $client->auth($dsn->getPassword())) {
                 throw new ConnectException('Could not connect authenticate connection to Redis database.');
             }
         }
         if ($dsn->getDatabase() !== null) {
             if (false === $client->select($dsn->getDatabase())) {
                 throw new ConnectException(sprintf('Could not select Redis database with index "%s".', $dsn->getDatabase()));
             }
         }
     }
     $pool = new RedisCachePool($client);
     if (null !== $config['pool_namespace']) {
         $pool = new NamespacedCachePool($pool, $config['pool_namespace']);
     }
     return $pool;
 }
Beispiel #21
0
 /**
  * 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();
     $YH =& get_instance();
     if ($YH->config->load('redis', TRUE, TRUE)) {
         $config += $YH->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('debug', 'Cache: Redis connection refused. Check the config.');
             return FALSE;
         }
     } catch (RedisException $e) {
         log_message('debug', 'Cache: Redis connection refused (' . $e->getMessage() . ')');
         return FALSE;
     }
     if (isset($config['password'])) {
         $this->_redis->auth($config['password']);
     }
     // Initialize the index of serialized values.
     $serialized = $this->_redis->sMembers('_ci_redis_serialized');
     if (!empty($serialized)) {
         $this->_serialized = array_flip($serialized);
     }
     return TRUE;
 }
Beispiel #22
0
 /**
  * Authenticates to the database (if needed) using options in the given configuration array.
  *
  * @return bool True if the authentication succeeded or no password was specified, else false
  */
 protected function _authenticate()
 {
     if ($this->config['password']) {
         return $this->_connection->auth($this->config['password']);
     }
     return true;
 }
 /**
  * 创建 Redis.io client
  * @param  array  $servers
  * @param  array  $options
  * @return array
  * @throw exception
  */
 protected function createClients(array $servers, array $options = [])
 {
     $clients = [];
     try {
         foreach ($servers as $key_s => $server) {
             $redis = new \Redis();
             //长连接为pconnect,长连接要注意执行close关闭
             $func = Arr::get($server, 'persistent', false) ? 'pconnect' : 'connect';
             $redis->connect(Arr::get($server, 'host', ''), Arr::get($server, 'port'), $this->timeOut);
             //有配置密码的,进行auth操作
             if ($pwd = Arr::get($server, 'password', '')) {
                 $redis->auth($pwd);
             }
             $redis->select(Arr::get($server, 'database'));
             //设置redis的option,如Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE
             foreach ($options as $key => $val) {
                 $redis->setOption($key, $val);
             }
             $clients[$key_s] = $redis;
         }
     } catch (\Exception $e) {
         throw new \Exception("connect redis error:" . var_export($e->getMessage(), 1));
     }
     return $clients;
 }
 /**
  * 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()
 {
     if (!$this->is_supported()) {
         log_message('error', 'Cache: Failed to create Redis object; extension not loaded?');
         return;
     }
     $CI =& get_instance();
     if ($CI->config->load('redis', TRUE, TRUE)) {
         $config = array_merge(self::$_default_config, $CI->config->item('redis'));
     } else {
         $config = self::$_default_config;
     }
     $this->_redis = new Redis();
     try {
         if (!$this->_redis->connect($config['host'], $config['host'][0] === '/' ? 0 : $config['port'], $config['timeout'])) {
             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.');
         }
         if (isset($config['database']) && $config['database'] > 0 && !$this->_redis->select($config['database'])) {
             log_message('error', 'Cache: Redis select database failed.');
         }
     } catch (RedisException $e) {
         log_message('error', 'Cache: Redis connection refused (' . $e->getMessage() . ')');
     }
 }
 /**
  * Gets the redis client
  * @return \Redis the redis client
  */
 public function getClient()
 {
     if ($this->_client === null) {
         $this->_client = new \Redis();
         $this->_client->connect($this->hostname, $this->port, $this->database);
         $slaveenable = false;
         if ($this->enableSlaves) {
             while (count($this->slaves) > 0) {
                 $this->_slave = new \Redis();
                 $select_slave = self::getByWeight($this->slaves);
                 //随机访问一台redis从服务器
                 $slaveenable = $this->_slave->connect($select_slave['hostname'], $select_slave['port'], $this->database);
                 if ($slaveenable) {
                     if (isset($select_slave['password'])) {
                         if ($this->_slave->auth($select_slave['password']) === false) {
                             throw new InvalidConfigException('Redis Slave authentication failed!');
                         }
                     }
                     break;
                 }
             }
         }
         if (!$slaveenable) {
             $this->_slave = $this->_client;
             $this->enableSlaves = false;
         }
         if (isset($this->password)) {
             if ($this->_client->auth($this->password) === false) {
                 throw new InvalidConfigException('Redis authentication failed!');
             }
         }
         //$this->_client->select($this->database);
     }
     return $this->_client;
 }
Beispiel #26
0
 /**
  * Create the Redis connection
  *
  * @return  Redis|boolean  Redis connection object on success, boolean on failure
  *
  * @since   3.4
  * @note    As of 4.0 this method will throw a JCacheExceptionConnecting object on connection failure
  */
 protected function getConnection()
 {
     if (static::isSupported() == false) {
         return false;
     }
     $config = JFactory::getConfig();
     $app = JFactory::getApplication();
     $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 (RedisException $e) {
             JLog::add($e->getMessage(), JLog::DEBUG);
         }
     } else {
         try {
             $connection = static::$_redis->connect($server['host'], $server['port']);
             $auth = !empty($server['auth']) ? static::$_redis->auth($server['auth']) : true;
         } catch (RedisException $e) {
             JLog::add($e->getMessage(), JLog::DEBUG);
         }
     }
     if ($connection == false) {
         static::$_redis = null;
         throw new JCacheExceptionConnecting('Redis connection failed', 500);
     }
     if ($auth == false) {
         static::$_redis = null;
         throw new JCacheExceptionConnecting('Redis authentication failed', 500);
     }
     $select = static::$_redis->select($server['db']);
     if ($select == false) {
         static::$_redis = null;
         throw new JCacheExceptionConnecting('Redis failed to select database', 500);
     }
     try {
         static::$_redis->ping();
     } catch (RedisException $e) {
         static::$_redis = null;
         throw new JCacheExceptionConnecting('Redis ping failed', 500);
     }
     return static::$_redis;
 }
Beispiel #27
0
 private function newInstance()
 {
     $r = new Redis();
     $r->connect(self::HOST, self::PORT);
     if (self::AUTH) {
         $this->assertTrue($r->auth(self::AUTH));
     }
     return $r;
 }
Beispiel #28
0
 /**
  * Connects to a Redis server
  *
  * @return boolean True if Redis server was connected
  */
 protected function _connect()
 {
     $return = false;
     try {
         $this->_Redis = new Redis();
         if (empty($this->settings['persistent'])) {
             $return = $this->_Redis->connect($this->settings['server'], $this->settings['port'], $this->settings['timeout']);
         } else {
             $return = $this->_Redis->pconnect($this->settings['server'], $this->settings['port'], $this->settings['timeout']);
         }
     } catch (RedisException $e) {
         return false;
     }
     if ($return && $this->settings['password']) {
         $return = $this->_Redis->auth($this->settings['password']);
     }
     return $return;
 }
Beispiel #29
0
 public function info()
 {
     //phpinfo();
     $redis = new Redis();
     $redis->connect(REDIS_HOST, REDIS_PORT);
     $redis->auth(REDIS_AUTH);
     $chat_data = $redis->lrange("chat:orFu-vgK-snskoQdDgMkBe-jFe1k:2", 0, -1);
     var_dump($chat_data);
 }
Beispiel #30
-1
 private function create()
 {
     $this->instance = new \Redis();
     // TODO allow configuring a RedisArray, see https://github.com/nicolasff/phpredis/blob/master/arrays.markdown#redis-arrays
     $config = $this->config->getValue('redis', array());
     if (isset($config['host'])) {
         $host = $config['host'];
     } else {
         $host = '127.0.0.1';
     }
     if (isset($config['port'])) {
         $port = $config['port'];
     } else {
         $port = 6379;
     }
     if (isset($config['timeout'])) {
         $timeout = $config['timeout'];
     } else {
         $timeout = 0.0;
         // unlimited
     }
     $this->instance->connect($host, $port, $timeout);
     if (isset($config['password']) && $config['password'] !== '') {
         $this->instance->auth($config['password']);
     }
     if (isset($config['dbindex'])) {
         $this->instance->select($config['dbindex']);
     }
 }