예제 #1
0
 /**
  * {@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;
 }
예제 #2
0
    /**
     * Returns the singleton instance of Redis. If no instance has
     * been created, a new instance will be created.
     *       
     *     $redis = Koredis::factory();
     *
     * @return Kohana_Koredis
     **/
    public static function factory()
    {
        if (!Kohana_Koredis::$redis) {
            // Make sure Redis installed
            if (!class_exists('Redis', FALSE)) {
                throw new Kohana_Exception('class Redis can not be found. make sure 
					you have installed phpredis extension');
            }
            Kohana_Koredis::$redis = new Redis();
            // No config file found
            if (!Kohana::$config->load('koredis')) {
                Kohana_Koredis::$redis->pconnect('127.0.0.1', 6379, 1);
            } else {
                // Load config
                $config = Kohana::$config->load('koredis');
                $host = isset($config['host']) && $config['host'] ? $config['host'] : '127.0.0.1';
                $port = isset($config['port']) && $config['port'] ? $config['port'] : 6379;
                $timeout = isset($config['timeout']) && $config['timeout'] ? $config['timeout'] : 1;
                $pconnect = isset($config['pconnect']) && $config['pconnect'] ? $config['pconnect'] : false;
                // Persistent connection
                if ($pconnect === TRUE) {
                    Kohana_Koredis::$redis->pconnect($host, $port, $timeout);
                } else {
                    Kohana_Koredis::$redis->connect($host, $port, $timeout);
                }
            }
        }
        return Kohana_Koredis::$redis;
    }
예제 #3
0
 /**
  * Constructor.
  *
  * @param string $uniqId to be used to separate objects in addition to their key identifiers, for instance in a
  * multi-user scenario.
  *
  * @return \iveeCrest\RedisWrapper
  */
 public function __construct($uniqId)
 {
     $this->redis = new \Redis();
     $this->redis->connect(Config::getCacheHost(), Config::getCachePort());
     $this->redis->setOption(\Redis::OPT_PREFIX, Config::getCachePrefix());
     $this->uniqId = $uniqId;
 }
예제 #4
0
 public function __construct()
 {
     $this->redis = new \Redis();
     if (!$this->redis->connect('127.0.0.1', 10000, 3)) {
         exit('redis connect failed');
     }
 }
예제 #5
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']);
 }
예제 #6
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;
 }
예제 #7
0
 /**
  * @param ProvidesConnectionData $connectionData
  *
  * @throws CannotConnectToServer
  */
 private function connectToServer(ProvidesConnectionData $connectionData)
 {
     $connected = $this->redis->connect($connectionData->getHost(), $connectionData->getPort(), $connectionData->getTimeout(), null, $connectionData->getRetryInterval());
     if (!$connected) {
         throw (new CannotConnectToServer())->withConnectionData($connectionData);
     }
 }
 public function __construct($redis = FALSE, $opts = array())
 {
     if (is_array($redis)) {
         $opts = $redis;
         $redis = FALSE;
     }
     // Apply default arguments
     $opts = array_merge(array('host' => 'localhost', 'port' => 6379), $opts);
     if (!$redis) {
         // Default to phpredis
         if (extension_loaded('redis')) {
             if (!isset($opts['socket']) && !isset($opts['host'])) {
                 throw new \Exception('Host should be provided when not providing a redis instance');
             }
             if (!isset($opts['socket']) && !isset($opts['port'])) {
                 throw new \Exception('Port should be provided when not providing a redis instance');
             }
             $redis = new \Redis();
             if (isset($opts['socket'])) {
                 $redis->connect($opts['socket']);
             } else {
                 $redis->connect($opts['host'], $opts['port']);
             }
         } else {
             $redis = new \TinyRedisClient($opts['host'] . ':' . $opts['port']);
         }
     }
     if (!is_callable(array($redis, 'publish'))) {
         throw new \Exception('The Redis client provided is invalid. The client needs to implement the publish method. Try using the default client.');
     }
     $this->redis = $redis;
     $this->key = (isset($opts['key']) ? $opts['key'] : 'socket.io') . '#/#';
     $this->_rooms = array();
     $this->_flags = array();
 }
예제 #9
0
 /**
  * @param \Redis $redis
  */
 public function __construct()
 {
     $this->redis = new \Redis();
     $this->redis->connect('127.0.0.1', '6379');
     $this->emitter = new Emitter($this->redis);
     $this->user = new Emitter($this->redis);
 }
예제 #10
0
 public static function class_init()
 {
     /** @var Settings $settings */
     $settings = resource(Settings::class);
     self::$redis = new \Redis();
     self::$redis->connect($settings->get("redis", "hostname"), $settings->get("redis", "connect_port"));
 }
예제 #11
0
 public function init()
 {
     $this->_redis = new \Redis();
     if (!$this->_redis->connect($this->host, $this->port)) {
         throw new InvalidConfigException("The redis host '{$this->host}' error.");
     }
 }
예제 #12
0
 /** 
  * Constructor
  *
  * This is the constructor. The options passed can be the
  * ip of the server, or an array of servers and their port.
  *
  * Example:
  * <code>
  *
  *     $options = array(
  *         'hostname' => '127.0.0.1',
  *         'port'     => 6666
  *     ); 
  *
  *     $cache   = Frapi_Cache_Adapter_Redis($options);
  * </code>
  *
  * @param array $options an array of options
  * @return void
  */
 public function __construct(array $options)
 {
     $this->redis = new Redis();
     $defaults = array('hostname' => '127.0.0.1', 'port' => 6379);
     $options += $defaults;
     $this->redis->connect($options['hostname'], $options['port']);
 }
예제 #13
0
 public function __construct($prefix = '')
 {
     parent::__construct($prefix);
     if (is_null(self::$cache)) {
         // TODO allow configuring a RedisArray, see https://github.com/nicolasff/phpredis/blob/master/arrays.markdown#redis-arrays
         self::$cache = new \Redis();
         $config = \OC::$server->getSystemConfig()->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
         }
         self::$cache->connect($host, $port, $timeout);
     }
 }
예제 #14
0
 public function setUp()
 {
     $this->redis = new Redis();
     $this->redis->connect('127.0.0.1', 6379);
     // uncomment the following if you want to use authentication
     // $this->assertTrue($this->redis->auth('foobared'));
 }
예제 #15
0
 /**
  * @param string $host redis server host
  * @param int $port redis server port
  * @param int $database redis server database num
  * @param string $channel redis queue key
  * @param string $prefix prefix of redis queue key
  */
 public function __construct($host = '127.0.0.1', $port = 6379, $database = 0, $channel = 'cache', $prefix = 'simple-fork-')
 {
     $this->redis = new \Redis();
     $connection_result = $this->redis->connect($host, $port);
     if (!$connection_result) {
         throw new \RuntimeException('can not connect to the redis server');
     }
     if ($database != 0) {
         $select_result = $this->redis->select($database);
         if (!$select_result) {
             throw new \RuntimeException('can not select the database');
         }
     }
     if (empty($channel)) {
         throw new \InvalidArgumentException('channel can not be empty');
     }
     $this->channel = $channel;
     if (empty($prefix)) {
         return;
     }
     $set_option_result = $this->redis->setOption(\Redis::OPT_PREFIX, $prefix);
     if (!$set_option_result) {
         throw new \RuntimeException('can not set the \\Redis::OPT_PREFIX Option');
     }
 }
예제 #16
0
 function init()
 {
     list($ip, $port) = explode(':', $this->_defaults['server']);
     $this->_handler = new Redis();
     $this->_handler->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY);
     $this->_handler->connect($ip, $port);
 }
예제 #17
0
 /**
  * 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;
 }
예제 #18
0
파일: redis.php 프로젝트: klas/joomla-cms
 /**
  * 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;
 }
예제 #19
0
 /**
  * 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);
     }
     return $this->_client;
 }
예제 #20
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);
     }
 }
예제 #21
0
 /**
  * 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;
 }
예제 #22
0
 public function __construct($connectionOrHost, $port = 6379)
 {
     if ($connectionOrHost instanceof \Redis) {
         $this->connection = $connectionOrHost;
     } else {
         $this->connection = new \Redis();
         $this->connection->connect($connectionOrHost, $port);
         $this->connection->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
     }
 }
 protected function setUp()
 {
     parent::setUp();
     if (!class_exists('\\Redis')) {
         $this->markTestSkipped("extension not loaded");
     }
     $this->_connection = new \Redis();
     $this->_connection->connect('localhost', 6379);
     $this->_adapter = new RedisAdapter($this->_connection);
 }
예제 #24
0
	public function __construct($host = '127.0.0.1', $port = '6379') {
		$this->_host = $host;
		$this->_port = $port;
		$this->_redis = new Redis();
		try {
			$this->_redis->connect($this->_host, $this->_port);
		} catch (Exception $e) {
			$this->_error = $e->getMessage();
		}

	}
 /**
  * @param array $config
  *
  * @return \Redis
  */
 private function getRedisConnection(array $config)
 {
     $redis = new \Redis();
     $host = isset($config['host']) ? $config['host'] : 'localhost';
     if (isset($config['port'])) {
         $redis->connect($host, $config['port']);
     } else {
         $redis->connect($host);
     }
     return $redis;
 }
예제 #26
0
 public function __construct()
 {
     $configPath = realpath(__DIR__ . '/../../../') . '/config.php';
     if (!file_exists($configPath)) {
         throw new \Exception('Config file missing');
     }
     $this->config = (require $configPath);
     $this->client = new Client($this->config['redis']['host'], $this->config['redis']['port']);
     $this->redis = new \Redis();
     $this->redis->connect($this->config['redis']['host'], $this->config['redis']['port']);
 }
예제 #27
0
 /**
  * Create queue
  *
  * @param array $options
  * @return Kue
  */
 public function __construct(array $options = array())
 {
     $this->injectors = $options + $this->injectors;
     $this->client =& $this->injectors['client'];
     if (!$this->client) {
         $this->client = new \Redis();
         $this->client->connect($this->injectors['host'], $this->injectors['port']);
         if ($this->injectors['db']) {
             $this->client->select($this->injectors['db']);
         }
     }
 }
예제 #28
0
 /**
  * @return \Redis
  */
 public function getRedis()
 {
     if (null === $this->redis) {
         if (class_exists('\\Redis')) {
             $this->redis = new \Redis();
             $this->redis->connect($this->params['host'], $this->params['port'], $this->params['timeout'], null, $this->params['retry_interval']);
         } else {
             throw new \RuntimeException('No Redis client is available');
         }
     }
     return $this->redis;
 }
예제 #29
0
 /**
  * 初始化Redis连接
  *
  * @throws RedisException
  */
 private function initRedis()
 {
     $apf = APF::get_instance();
     $config = $apf->get_config('house_updown', 'redis');
     if (!isset($config['host']) || !isset($config['port'])) {
         throw new RedisException('Host and port are needed to connect redis server.');
     }
     $this->redis = new Redis();
     if (!$this->redis->connect($config['host'], $config['port'])) {
         throw new RedisException("Failed to connect redis server: {$config['host']}:{$config['port']}");
     }
 }
예제 #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']);
     }
 }