connect() public method

Opens the underlying connection and connects to the server.
public connect ( )
コード例 #1
0
ファイル: RedisCopy.php プロジェクト: becquerel/redis-copy
 /**
  * @param $sourceConfig connection URI string (tcp://10.0.0.1:6379)
  * @param $destinationConfig connection URI string (tcp://10.0.0.1:6379)
  * @throws Predis\Connection\ConnectionException
  */
 public function __construct($sourceConfig, $destinationConfig)
 {
     $this->source = new Client($sourceConfig);
     $this->destination = new Client($destinationConfig);
     $this->source->connect();
     $this->destination->connect();
 }
コード例 #2
0
 public function fork()
 {
     $id = pcntl_fork();
     if (!$id) {
         $this->redis->disconnect();
         $this->redis->connect();
     }
     return $id;
 }
コード例 #3
0
 /**
  * Creates a connect to Redis or Sentinel using the Predis\Client object.  It proxies the connecting and converts
  * specific client exceptions to more generic adapted ones in PSRedis
  *
  * @throws \PSRedis\Exception\ConnectionError
  */
 public function connect()
 {
     try {
         $this->predisClient = $this->predisClientFactory->createClient($this->clientType, $this->getPredisClientParameters());
         $this->predisClient->connect();
         $this->isConnected = $this->predisClient->isConnected();
     } catch (ConnectionException $e) {
         throw new ConnectionError($e->getMessage());
     }
 }
コード例 #4
0
ファイル: RedisBroker.php プロジェクト: ekowabaka/ajumamoro
 public function init()
 {
     $settings = Config::get('ajumamoro:broker');
     unset($settings['driver']);
     $this->redis = new \Predis\Client($settings);
     try {
         $this->redis->connect();
     } catch (\Predis\CommunicationException $ex) {
         throw new BrokerConnectionException("Failed to connect to redis broker: {$ex->getMessage()}");
     }
 }
コード例 #5
0
 /**
  * @return bool
  */
 public function checkConnection()
 {
     if (!$this->predis->isConnected()) {
         try {
             $this->predis->connect();
             return true;
         } catch (ConnectionException $e) {
             return false;
         }
     }
     return true;
 }
コード例 #6
0
 function __construct($url)
 {
     try {
         list($user, $passwd, $host, $port, $db) = self::parseRedisUrl($url);
         $this->_redis = new Client(array('host' => $host, 'port' => $port));
     } catch (\Exception $e) {
         throw new RedisBundleException($e);
     }
     try {
         $this->_redis->connect();
         $this->_redis->select($db);
     } catch (\Predis\Connection\ConnectionException $e) {
         throw new RedisBundleException($e);
     }
 }
コード例 #7
0
 /**
  * @group connected
  */
 public function testDispatcherLoopAgainstRedisServer()
 {
     $parameters = array('host' => REDIS_SERVER_HOST, 'port' => REDIS_SERVER_PORT, 'database' => REDIS_SERVER_DBNUM, 'read_write_timeout' => 2);
     $producer = new Client($parameters, REDIS_SERVER_VERSION);
     $producer->connect();
     $consumer = new Client($parameters, REDIS_SERVER_VERSION);
     $consumer->connect();
     $dispatcher = new DispatcherLoop($consumer);
     $function01 = $this->getMock('stdClass', array('__invoke'));
     $function01->expects($this->exactly(2))->method('__invoke')->with($this->logicalOr($this->equalTo('01:argument'), $this->equalTo('01:quit')))->will($this->returnCallback(function ($arg) use($dispatcher) {
         if ($arg === '01:quit') {
             $dispatcher->stop();
         }
     }));
     $function02 = $this->getMock('stdClass', array('__invoke'));
     $function02->expects($this->once())->method('__invoke')->with('02:argument');
     $function03 = $this->getMock('stdClass', array('__invoke'));
     $function03->expects($this->never())->method('__invoke');
     $dispatcher->attachCallback('function:01', $function01);
     $dispatcher->attachCallback('function:02', $function02);
     $dispatcher->attachCallback('function:03', $function03);
     $producer->publish('function:01', '01:argument');
     $producer->publish('function:02', '02:argument');
     $producer->publish('function:01', '01:quit');
     $dispatcher->run();
     $this->assertTrue($consumer->ping());
 }
コード例 #8
0
 public function testHandleLogWithGoodMessageNotImplementingJobInterface()
 {
     $worker = new WorkerPresence();
     $worker->setMemory(12345);
     $frame = new Frame('MESSAGE', array('delivery_tag' => 'delivery-' . mt_rand()), $worker->toJson());
     $loop = LoopFactory::create();
     $options = array('eventloop' => $loop, 'on_error' => array($this, 'throwRedisError'));
     $redisSync = new PredisSync('tcp://127.0.0.1:6379');
     $redisSync->connect();
     $resolver = $this->getResolver();
     $resolver->expects($this->once())->method('ack');
     $done = false;
     $phpunit = $this;
     $redis = new PredisAsync('tcp://127.0.0.1:6379', $options);
     $redis->connect(function () use($resolver, $phpunit, $redis, $frame, $redisSync, &$done, $worker) {
         $component = new LogBuilderComponent();
         $component->handleLog($redis, $phpunit->getLogger(), $frame, $resolver)->then(function ($hashId) use($phpunit, $redis, $redisSync, &$done, $worker) {
             $redis->disconnect();
             $phpunit->assertEquals($worker->toJson(), $redisSync->get($hashId));
             $phpunit->assertTrue($redisSync->sismember('garbages', $hashId));
             $done = true;
         });
     });
     $loop->run();
     $this->assertTrue($done);
 }
コード例 #9
0
 /**
  * Returns a new client instance.
  *
  * @return Client
  */
 protected function getClient()
 {
     $parameters = array('host' => REDIS_SERVER_HOST, 'port' => REDIS_SERVER_PORT, 'iterable_multibulk' => true, 'read_write_timeout' => 2);
     $client = new Client($parameters, REDIS_SERVER_VERSION);
     $client->connect();
     $client->select(REDIS_SERVER_DBNUM);
     $client->flushdb();
     return $client;
 }
コード例 #10
0
 /**
  * ### Tries to connect to the configured redis database
  *
  * RedisCache constructor.
  */
 public function __construct()
 {
     $client = new Client(['scheme' => 'tcp', 'host' => Config::get('cache', 'redis_server.host'), 'port' => Config::get('cache', 'redis_server.port'), 'database' => Config::get('cache', 'redis_server.database')]);
     try {
         $client->connect();
     } catch (\Exception $e) {
         throw new RedisException('Connection to redis server failed. Please check your settings.');
     }
     $this->conn = $client;
 }
コード例 #11
0
 public static function setUpBeforeClass()
 {
     parent::setUpBeforeClass();
     $client = new Client();
     try {
         $client->connect();
         $client->disconnect();
         self::$supported = true;
     } catch (ConnectionException $e) {
         self::$supported = false;
     }
 }
コード例 #12
0
ファイル: RedisTest.php プロジェクト: Flesh192/magento
 public function setUp()
 {
     // connect to a redis daemon
     $predis = new Predis(array('host' => $_ENV['redis_host'], 'port' => $_ENV['redis_port']));
     // set it
     $this->storage = new Redis($predis, 'test_user_token', 'test_user_state');
     try {
         $predis->connect();
     } catch (\Predis\Connection\ConnectionException $e) {
         $this->markTestSkipped('No redis instance available: ' . $e->getMessage());
     }
 }
コード例 #13
0
 public function setUp()
 {
     if (!class_exists('Predis\\Client')) {
         $this->markTestSkipped('The ' . __CLASS__ . ' requires Predis to be available');
     }
     try {
         $client = new Client();
         $client->connect();
         $client->flushdb();
     } catch (ConnectionException $e) {
         $this->markTestSkipped('The ' . __CLASS__ . ' requires the use of a Redis Server');
     }
     $this->storage = new RedisStorage($client);
 }
コード例 #14
0
 /**
  * Returns a new client instance.
  *
  * @param Boolean $connect Flush selected database before returning the client.
  * @return Client
  */
 protected function getClient($flushdb = true)
 {
     $profile = $this->getProfile();
     if (!$profile->supportsCommand($id = $this->getExpectedId())) {
         $this->markTestSkipped("The profile {$profile->getVersion()} does not support command {$id}");
     }
     $parameters = array('host' => REDIS_SERVER_HOST, 'port' => REDIS_SERVER_PORT);
     $options = array('profile' => $profile);
     $client = new Client($parameters, $options);
     $client->connect();
     $client->select(REDIS_SERVER_DBNUM);
     if ($flushdb) {
         $client->flushdb();
     }
     return $client;
 }
コード例 #15
0
 public function test_published_messages_fire_event_listeners_when_rechech_called()
 {
     $redis = new Client();
     $pubsub = new RedisPubSub($redis);
     try {
         $redis->connect();
     } catch (ConnectionException $e) {
         $this->markTestSkipped("Test skipped because no Redis server is running on default ports");
     }
     $value = null;
     $pubsub->on('foo', function ($data) use(&$value) {
         $value = $data;
     });
     $pubsub->publish('foo', 'bar');
     $pubsub->recheck('foo');
     $this->assertEquals('bar', $value);
 }
コード例 #16
0
 /**
  * @group connected
  */
 public function testDispatcherLoopAgainstRedisServerWithPrefix()
 {
     $parameters = array('host' => REDIS_SERVER_HOST, 'port' => REDIS_SERVER_PORT, 'database' => REDIS_SERVER_DBNUM, 'read_write_timeout' => 2);
     $options = array('profile' => REDIS_SERVER_VERSION);
     $producerNonPfx = new Client($parameters, $options);
     $producerNonPfx->connect();
     $producerPfx = new Client($parameters, $options + array('prefix' => 'foobar'));
     $producerPfx->connect();
     $consumer = new Client($parameters, $options + array('prefix' => 'foobar'));
     $dispatcher = new DispatcherLoop($consumer);
     $callback = $this->getMock('stdClass', array('__invoke'));
     $callback->expects($this->exactly(1))->method('__invoke')->with($this->equalTo('arg:prefixed'))->will($this->returnCallback(function ($arg) use($dispatcher) {
         $dispatcher->stop();
     }));
     $dispatcher->attachCallback('callback', $callback);
     $producerNonPfx->publish('callback', 'arg:non-prefixed');
     $producerPfx->publish('callback', 'arg:prefixed');
     $dispatcher->run();
     $this->assertTrue($consumer->ping());
 }
コード例 #17
0
 /**
  * Returns a new client instance.
  *
  * @return Client
  */
 protected function getClient()
 {
     $parameters = $this->getParameters(array('read_write_timeout' => 2));
     $protocol = new TextProtocolProcessor();
     $protocol->useIterableMultibulk(true);
     $connection = new CompositeStreamConnection($parameters, $protocol);
     $client = new Client($connection);
     $client->connect();
     $client->flushdb();
     return $client;
 }
コード例 #18
0
ファイル: Client.php プロジェクト: Antevenio/redis
 public function connect()
 {
     $this->_client->connect();
 }
コード例 #19
0
ファイル: Predis.php プロジェクト: tonicospinelli/disque-php
 /**
  * @inheritdoc
  */
 public function connect($connectionTimeout = null, $responseTimeout = null)
 {
     parent::connect($connectionTimeout, $responseTimeout);
     $this->client = $this->buildClient($this->host, $this->port);
     $this->client->connect();
 }
コード例 #20
0
 /**
  * @group connected
  * @requires extension pcntl
  */
 public function testPubSubAgainstRedisServerBlocking()
 {
     $parameters = array('host' => REDIS_SERVER_HOST, 'port' => REDIS_SERVER_PORT, 'database' => REDIS_SERVER_DBNUM, 'read_write_timeout' => -1);
     $options = array('profile' => REDIS_SERVER_VERSION);
     // create consumer before forking so the child can disconnect it
     $consumer = new Client($parameters, $options);
     $consumer->connect();
     /*
      * fork
      *  parent: consumer
      *  child: producer
      */
     if ($childPID = pcntl_fork()) {
         $messages = array();
         $pubsub = new PubSubConsumer($consumer);
         $pubsub->subscribe('channel:foo');
         foreach ($pubsub as $message) {
             if ($message->kind !== 'message') {
                 continue;
             }
             $messages[] = $payload = $message->payload;
             if ($payload === 'QUIT') {
                 $pubsub->stop();
             }
         }
         $this->assertSame(array('message1', 'message2', 'QUIT'), $messages);
         $this->assertFalse($pubsub->valid());
         $this->assertEquals('ECHO', $consumer->echo('ECHO'));
         // kill the child
         posix_kill($childPID, SIGKILL);
     } else {
         // create producer, read_write_timeout = 2 because it doesn't do blocking reads anyway
         $producer = new Client(array_replace($parameters, array('read_write_timeout' => 2)), $options);
         $producer->connect();
         $producer->publish('channel:foo', 'message1');
         $producer->publish('channel:foo', 'message2');
         $producer->publish('channel:foo', 'QUIT');
         // sleep, giving the consumer a chance to respond to the QUIT message
         sleep(1);
         // disconnect the consumer because otherwise it could remain stuck in blocking read
         //  if it failed to respond to the QUIT message
         $consumer->disconnect();
         // exit child
         exit(0);
     }
 }
コード例 #21
0
ファイル: Predis.php プロジェクト: Revisor/disque-php
 /**
  * Connect
  *
  * @param array $options Connection options
  */
 public function connect(array $options = [])
 {
     parent::connect($options);
     $this->client = $this->buildClient($this->host, $this->port);
     $this->client->connect();
 }
コード例 #22
0
 /**
  * @group connected
  */
 public function testPubSubAgainstRedisServer()
 {
     $parameters = array('host' => REDIS_SERVER_HOST, 'port' => REDIS_SERVER_PORT, 'database' => REDIS_SERVER_DBNUM, 'read_write_timeout' => 2);
     $options = array('profile' => REDIS_SERVER_VERSION);
     $messages = array();
     $producer = new Client($parameters, $options);
     $producer->connect();
     $consumer = new Client($parameters, $options);
     $consumer->connect();
     $pubsub = new PubSubContext($consumer);
     $pubsub->subscribe('channel:foo');
     $producer->publish('channel:foo', 'message1');
     $producer->publish('channel:foo', 'message2');
     $producer->publish('channel:foo', 'QUIT');
     foreach ($pubsub as $message) {
         if ($message->kind !== 'message') {
             continue;
         }
         $messages[] = $payload = $message->payload;
         if ($payload === 'QUIT') {
             $pubsub->closeContext();
         }
     }
     $this->assertSame(array('message1', 'message2', 'QUIT'), $messages);
     $this->assertFalse($pubsub->valid());
     $this->assertTrue($consumer->ping());
 }
コード例 #23
0
ファイル: redis.php プロジェクト: weareathlon/silla.io
 /**
  * Redis constructor.
  */
 public function __construct()
 {
     $connParams = Core\Config()->CACHE['redis'];
     $this->redisClient = new Predis\Client($connParams);
     $this->redisClient->connect();
 }
コード例 #24
0
 /**
  * Connects to client
  */
 public function connect()
 {
     $this->client = $this->clientFactory->createClient($this->getClientParameters());
     $this->client->connect();
 }
コード例 #25
0
ファイル: Bootstrap.php プロジェクト: johnmicahmiguel/yodaphp
 private function testRedisConnection(\Predis\Client $redis)
 {
     try {
         $redis->connect();
     } catch (\Predis\CommunicationException $exception) {
         // we were unable to connect to the redis server
         return false;
     }
     return true;
 }
コード例 #26
0
 /**
  * Connects to client
  */
 public function connect()
 {
     $this->client = new \Redis();
     $this->isConnected = $this->client->connect($this->getHost(), $this->getPort());
 }
コード例 #27
0
ファイル: ClientTest.php プロジェクト: rodrigopbel/ong
 /**
  * @group disconnected
  */
 public function testConnectAndDisconnect()
 {
     $connection = $this->getMock('Predis\\Connection\\ConnectionInterface');
     $connection->expects($this->once())->method('connect');
     $connection->expects($this->once())->method('disconnect');
     $client = new Client($connection);
     $client->connect();
     $client->disconnect();
 }
コード例 #28
0
 /**
  * Returns a new client instance.
  *
  * @param  array  $parameters Additional connection parameters.
  * @param  array  $options    Additional client options.
  * @param  bool   $flushdb    Flush selected database before returning the client.
  * @return Client
  */
 protected function createClient(array $parameters = null, array $options = null, $flushdb = true)
 {
     $parameters = array_merge($this->getDefaultParametersArray(), $parameters ?: array());
     $options = array_merge(array('profile' => $this->getProfile()), $options ?: array());
     $client = new Client($parameters, $options);
     $client->connect();
     if ($flushdb) {
         $client->flushdb();
     }
     return $client;
 }
コード例 #29
0
 /**
  * @group connected
  */
 public function testMonitorAgainstRedisServer()
 {
     $parameters = array('host' => REDIS_SERVER_HOST, 'port' => REDIS_SERVER_PORT, 'database' => REDIS_SERVER_DBNUM, 'read_write_timeout' => 2);
     $options = array('profile' => REDIS_SERVER_VERSION);
     $echoed = array();
     $producer = new Client($parameters, $options);
     $producer->connect();
     $consumer = new Client($parameters, $options);
     $consumer->connect();
     $monitor = new MonitorContext($consumer);
     $producer->echo('message1');
     $producer->echo('message2');
     $producer->echo('QUIT');
     foreach ($monitor as $message) {
         if ($message->command == 'ECHO') {
             $echoed[] = $arguments = trim($message->arguments, '"');
             if ($arguments == 'QUIT') {
                 $monitor->closeContext();
             }
         }
     }
     $this->assertSame(array('message1', 'message2', 'QUIT'), $echoed);
     $this->assertFalse($monitor->valid());
     $this->assertTrue($consumer->ping());
 }
コード例 #30
0
 /**
  * Returns a client instance connected to the specified Redis
  * server instance to perform integration tests.
  *
  * @param array Additional connection parameters.
  * @param array Additional client options.
  * @return Client client instance.
  */
 protected function getClient(array $parameters = array(), array $options = array())
 {
     $parameters = array_merge(array('scheme' => 'tcp', 'host' => REDIS_SERVER_HOST, 'port' => REDIS_SERVER_PORT, 'database' => REDIS_SERVER_DBNUM), $parameters);
     $options = array_merge(array('profile' => REDIS_SERVER_VERSION), $options);
     $client = new Client($parameters, $options);
     $client->connect();
     $client->flushdb();
     return $client;
 }