本文整理汇总了PHP中Redis::connect方法的典型用法代码示例。如果您正苦于以下问题:PHP Redis::connect方法的具体用法?PHP Redis::connect怎么用?PHP Redis::connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Redis
的用法示例。
在下文中一共展示了Redis::connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getAdapter
/**
* {@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: factory
/**
* 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: __construct
/**
* 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: __construct
public function __construct()
{
$this->redis = new \Redis();
if (!$this->redis->connect('127.0.0.1', 10000, 3)) {
exit('redis connect failed');
}
}
示例5: _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']);
}
示例6: _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;
}
示例7: connectToServer
/**
* @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);
}
}
示例8: __construct
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: __construct
/**
* @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: class_init
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: init
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: __construct
/**
* 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: __construct
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: setUp
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: __construct
/**
* @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');
}
}