/** * @return RedisClient */ public function getRedis() { if (!$this->Redis) { $this->Redis = ClientFactory::create($this->options); } return $this->Redis; }
/** * */ public function test_RedisVersions() { foreach ($this->versions as $n => $arr) { list($server, $clientVersion, $serverVersion) = $arr; $Redis = ClientFactory::create(['server' => $server, 'timeout' => 2, 'version' => $clientVersion, 'password' => $n % 2 ? TEST_REDIS_SERVER_PASSWORD : null]); $this->assertSame($clientVersion, $Redis->getSupportedVersion()); $redisVersion = $Redis->info('Server')['redis_version']; $expectedVersion = $this->getRegExp($serverVersion); try { $this->assertSame(1, preg_match($expectedVersion, $redisVersion)); } catch (\Exception $Ex) { throw new \Exception(sprintf('Incorrect Redis Server Version for [%s], expected: %s, given: %s', $server, $serverVersion, $redisVersion)); } } }
/** * @see RedisProtocol::createClientByVersion */ public function test_createClientByVersion() { $this->assertInstanceOf(RedisClient2x6::class, ClientFactory::create(['version' => '2.6'])); $this->assertInstanceOf(RedisClient2x6::class, ClientFactory::create()); $this->assertInstanceOf(RedisClient3x2::class, ClientFactory::createClientByVersion(3.2)); $this->assertInstanceOf(RedisClient3x2::class, ClientFactory::createClientByVersion('3.2.2')); $this->assertInstanceOf(RedisClient3x2::class, ClientFactory::createClientByVersion('3.2.0')); $this->assertInstanceOf(RedisClient3x2::class, ClientFactory::createClientByVersion('3.2.x')); $this->assertInstanceOf(RedisClient3x2::class, ClientFactory::createClientByVersion('3.1')); $this->assertInstanceOf(RedisClient3x2::class, ClientFactory::createClientByVersion('3.1.5')); $this->assertInstanceOf(RedisClient2x6::class, ClientFactory::create()); $this->assertInstanceOf(RedisClient3x0::class, ClientFactory::createClientByVersion('3.0')); $this->assertInstanceOf(RedisClient3x0::class, ClientFactory::createClientByVersion(3.0)); $this->assertInstanceOf(RedisClient3x0::class, ClientFactory::createClientByVersion('3')); $this->assertInstanceOf(RedisClient3x0::class, ClientFactory::createClientByVersion(3)); $this->assertInstanceOf(RedisClient3x0::class, ClientFactory::createClientByVersion('3.0.9')); $this->assertInstanceOf(RedisClient3x0::class, ClientFactory::createClientByVersion('3.0.6')); $this->assertInstanceOf(RedisClient3x0::class, ClientFactory::createClientByVersion('3.0.x')); $this->assertInstanceOf(RedisClient3x0::class, ClientFactory::createClientByVersion('2.9')); $this->assertInstanceOf(RedisClient3x0::class, ClientFactory::createClientByVersion(2.9)); $this->assertInstanceOf(RedisClient3x0::class, ClientFactory::createClientByVersion('2.9.1')); $this->assertInstanceOf(RedisClient2x8::class, ClientFactory::createClientByVersion('2.8')); $this->assertInstanceOf(RedisClient2x8::class, ClientFactory::createClientByVersion(2.8)); $this->assertInstanceOf(RedisClient2x8::class, ClientFactory::createClientByVersion('2.8.24')); $this->assertInstanceOf(RedisClient2x8::class, ClientFactory::createClientByVersion('2.7')); $this->assertInstanceOf(RedisClient2x8::class, ClientFactory::createClientByVersion(2.7)); $this->assertInstanceOf(RedisClient2x8::class, ClientFactory::createClientByVersion('2.7.4')); $this->assertInstanceOf(RedisClient2x6::class, ClientFactory::createClientByVersion('2.6')); $this->assertInstanceOf(RedisClient2x6::class, ClientFactory::createClientByVersion(2.6)); $this->assertInstanceOf(RedisClient2x6::class, ClientFactory::createClientByVersion('2.6.17')); $this->assertInstanceOf(RedisClient2x6::class, ClientFactory::createClientByVersion('2.6.17')); $this->assertInstanceOf(RedisClient2x6::class, ClientFactory::createClientByVersion('2.5')); $this->assertInstanceOf(RedisClient2x6::class, ClientFactory::createClientByVersion(2.5)); $this->assertInstanceOf(RedisClient2x6::class, ClientFactory::createClientByVersion('2')); $this->assertInstanceOf(RedisClient2x6::class, ClientFactory::createClientByVersion(2)); $this->assertInstanceOf(RedisClient2x6::class, ClientFactory::create()); }
* Create a new instance of RedisClient */ namespace Examples; require dirname(__DIR__) . '/vendor/autoload.php'; // or require (dirname(__DIR__).'/src/autoloader.php'); use RedisClient\RedisClient; use RedisClient\Client\Version\RedisClient2x6; use RedisClient\ClientFactory; // Example 1. Create new Instance with default config $Redis = new RedisClient(); // By default, the client works with the latest stable version of Redis. echo 'RedisClient: ' . $Redis->getSupportedVersion() . PHP_EOL; echo 'Redis: ' . $Redis->info('Server')['redis_version'] . PHP_EOL; // RedisClient: 3.2 // Redis: 3.0.3 // Example 2. Create new Instance with config // By default, the client works with the latest stable version of Redis. $Redis = new RedisClient(['server' => 'tcp://127.0.0.1:6387', 'timeout' => 2]); echo 'RedisClient: ' . $Redis->getSupportedVersion() . PHP_EOL; echo 'Redis: ' . $Redis->info('Server')['redis_version'] . PHP_EOL; // RedisClient: 3.2 // Redis: 3.2.0 // Example 3. Create new Instance for Redis version 2.6.x with config $Redis = new RedisClient2x6(['server' => 'tcp://127.0.0.1:6379', 'timeout' => 2]); echo 'RedisClient: ' . $Redis->getSupportedVersion() . PHP_EOL; // RedisClient: 2.6 // Example 4. Create new Instance for Redis version 2.8.x with config via factory $Redis = ClientFactory::create(['server' => 'tcp://127.0.0.1:6379', 'timeout' => 2, 'version' => '2.8.24']); echo 'RedisClient: ' . $Redis->getSupportedVersion() . PHP_EOL; // RedisClient: 2.8
/** * @return RedisClient */ protected function getRedis() { return ClientFactory::create(['server' => TEST_REDIS_SERVER]); }
public static function setUpBeforeClass() { static::$Redis = ClientFactory::create(['server' => REDIS_TEST_SERVER]); }
/** * @return RedisClient */ protected function getRedis() { return ClientFactory::create(); }
<?php require 'vendor/autoload.php'; use RedisLock\RedisLock; use RedisClient\ClientFactory; use RedisClient\RedisClient; // Create a new Redis instance $Redis = ClientFactory::create(['server' => 'tcp://127.0.0.1:6379']); // ... /** * Safe update json in Redis storage * @param Redis $Redis * @param string $key * @param array $array * @throws Exception */ function updateJsonInRedis(RedisClient $Redis, $key, array $array) { // Create new Lock instance $Lock = new RedisLock($Redis, 'Lock_' . $key, RedisLock::FLAG_CATCH_EXCEPTIONS); // Acquire lock for 2 sec. // If lock has acquired in another thread then we will wait 3 second, // until another thread release the lock. Otherwise it throws a exception. if (!$Lock->acquire(2, 3)) { throw new Exception('Can\'t get a Lock'); } // Get value from storage $json = $Redis->get($key); if (!$json) { $jsonArray = []; } else {