本文整理汇总了PHP中Redis::select方法的典型用法代码示例。如果您正苦于以下问题:PHP Redis::select方法的具体用法?PHP Redis::select怎么用?PHP Redis::select使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Redis
的用法示例。
在下文中一共展示了Redis::select方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __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');
}
}
示例2: 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;
}
示例3: getClient
/**
* 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->_client->select($this->database);
}
return $this->_client;
}
示例4: __construct
/**
* 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']);
}
}
}
示例5: _connect
protected function _connect()
{
$this->_redis = new \Redis();
if ($this->_config['persistent']) {
$this->_redis->pconnect($this->_config['host'], isset($this->_config['port']) ? $this->_config['port'] : null, isset($this->_config['timeout']) ? $this->_config['timeout'] : null);
} else {
$this->_redis->connect($this->_config['host'], isset($this->_config['port']) ? $this->_config['port'] : null, isset($this->_config['timeout']) ? $this->_config['timeout'] : null);
}
$this->_redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_NONE);
if ($this->_config['database'] > 0) {
$this->_redis->select($this->_config['database']);
}
}
示例6: getRedis
/**
* Return redis resource
*
* @return Redis
*/
public function getRedis()
{
if (!$this->redis) {
$this->redis = new Redis();
if (isset($this->socket)) {
$this->redis->connect($this->socket);
} else {
$this->redis->connect($this->host, $this->port);
}
$this->redis->select($this->database);
}
return $this->redis;
}
示例7: factory
/**
* Get instance redis
*
* @return Redis
*/
public static function factory()
{
if (!self::$instance) {
try {
self::$instance = new Redis();
self::$instance->connect(Config::get('host'), Config::get('port'), Config::get('timeout'));
self::$instance->select(Request::factory()->getDb());
} catch (RedisException $e) {
throw $e;
}
}
return self::$instance;
}
示例8: 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);
}
}
示例9: 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;
}
示例10: __construct
public function __construct($config)
{
if (!is_array($config) || array_key_exists('port', $config) && (!is_numeric($config['port']) || intval($config['port']) <= 0 || intval($config['port']) > 65535) || array_key_exists('index', $config) && (!is_numeric($config['index']) || intval($config['index']) < 0 || intval($config['index']) > 15) || array_key_exists('timeout', $config) && (!is_numeric($config['timeout']) || intval($config['timeout']) < 0)) {
throw new \InvalidArgumentException();
}
$ip = array_key_exists('ip', $config) ? $config['ip'] : self::DEFAULT_IP;
$retry = false;
$oriConfig = $ip;
if (is_array($ip)) {
if (count($ip) > 1) {
$retry = true;
}
$tmpIdx = array_rand($ip);
$ip = $ip[$tmpIdx];
unset($oriConfig[$tmpIdx]);
}
$port = array_key_exists('port', $config) ? intval($config['port']) : self::DEFAULT_PORT;
$index = array_key_exists('index', $config) ? intval($config['index']) : self::DEFAULT_INDEX;
$keepalive = array_key_exists('keepalive', $config) ? (bool) $config['keepalive'] : self::DEFAULT_KEEPALIVE;
$timeout = array_key_exists('timeout', $config) ? doubleval($config['timeout']) / 1000 : self::DEFAULT_TIMEOUT;
$instance = new \Redis();
$connectSucceeded = false;
if ($keepalive) {
$connectSucceeded = $instance->pconnect($ip, $port, $timeout);
} else {
$connectSucceeded = $instance->connect($ip, $port, $timeout);
}
if (!$connectSucceeded || !$instance->select($index)) {
if ($retry) {
$tmpIdx = array_rand($oriConfig);
$ip = $oriConfig[$tmpIdx];
if ($keepalive) {
$connectSucceeded = $instance->pconnect($ip, $port, $timeout);
} else {
$connectSucceeded = $instance->connect($ip, $port, $timeout);
}
if (!$connectSucceeded || !$instance->select($index)) {
self::logConnectionError($config);
throw new \RedisException();
}
} else {
self::logConnectionError($config);
throw new \RedisException();
}
}
$this->redis = $instance;
return;
}
示例11: selectDatabase
/**
* @param unknown $database
*
* @throws \Mcustiel\SimpleCache\Drivers\phpredis\Exceptions\RedisConnectionException
*/
private function selectDatabase($database)
{
if (!is_integer($database) || $database < 0) {
throw new RedisConnectionException("Can't select database '{$database}'. Should be a natural number.");
}
$this->connection->select($database);
}
示例12: _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;
}
示例13: process
public function process()
{
$this->params['output'] = 'json';
$context = \CADB\Model\Context::instance();
if (!($rdb = $context->getProperty('service.redis'))) {
$this->result = array('found' => false, 'error' => "자동완성 기능이 활성화되어 있지 않습니다.");
} else {
if (!$this->params['q']) {
$this->result = array('found' => false, 'error' => "자동완성할 키워드를 입력하세요.");
} else {
$redis = new \Redis();
try {
$redis->connect('127.0.0.1', '6379', 2.5, NULL, 150);
if ($redis->select($rdb) == false) {
$this->result = array('found' => false, 'error' => "index 1 database 에 연결할 수 없습니다.");
} else {
$this->recommand = $redis->zRange($this->params['q'], 0, -1);
if (@count($this->recommand)) {
$this->result = array('found' => true, 'total_cnt' => @count($this->recommand));
} else {
$this->result = array('found' => true, 'total_cnt' => 0);
}
}
} catch (RedisException $e) {
var_dump($e);
}
$redis->close();
}
}
}
示例14: connect
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;
}
示例15: openConnection
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;
}