Example #1
0
 /**
  * Try to connect to the cache server and make read/write operations
  */
 public function testCacheAccess()
 {
     $cc = new Redis($this->settings);
     $this->assertEquals(null, $cc->getData('tests-' . $this->session . '-string'));
     $cc->setData('tests-' . $this->session . '-string', 'this is a simple string');
     $this->assertEquals('this is a simple string', $cc->getData('tests-' . $this->session . '-string'));
     $cc->setData('tests-' . $this->session . '-int', 3);
     $this->assertEquals(3, $cc->getData('tests-' . $this->session . '-int'));
     $cc->setData('tests-' . $this->session . '-float', 3.3);
     $this->assertEquals(3.3, $cc->getData('tests-' . $this->session . '-float'));
     $cc->setData('tests-' . $this->session . '-array', ['a', 'b']);
     $this->assertEquals(['a', 'b'], $cc->getData('tests-' . $this->session . '-array'));
 }
Example #2
0
 /**
  * Connects to the Redis server
  *
  * @param Setting $settings Setting instance with the required attributes
  * @return Redis Current object to daisy chain method calls
  *
  * @throws Exception When the Redis server was not reached
  */
 private function doConnect(Setting $settings) : self
 {
     # Connection
     $connection = new \Redis();
     $connection->connect($settings->getSetting('address'), $settings->getSetting('port'), $settings->getSetting('timeout'));
     # When error occurred
     if ($connection->getLastError() !== null) {
         throw new Exception('Failed to connect to the Redis server: ' . $connection->getLastError());
     }
     # Set options
     $connection->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
     # Store the connection
     $this->connection = $connection;
     return $this;
 }