From d02c04c74297ee2a321bdd637160e14672f28e07 Mon Sep 17 00:00:00 2001 From: Alexander Cheprasov Date: Sun, 24 Jan 2016 19:11:58 +0000 Subject: [PATCH] v1.0-rc1 --- README.md | 24 ++++++- examples/create_new_instance.php | 32 ++++++---- examples/monitor.php | 6 +- examples/pipeline.php | 6 +- examples/pubsub.php | 6 +- examples/raw_commands.php | 41 ++++++++++++ examples/transactions.php | 6 +- .../Client/Version/RedisClient2x6.php | 4 -- .../Client/Version/RedisClient2x8.php | 4 -- .../Client/Version/RedisClient3x0.php | 4 -- .../Client/Version/RedisClient3x2.php | 4 -- src/RedisClient/ClientFactory.php | 63 +++++++++++++++++++ .../Command/Traits/AbstractCommandsTrait.php | 2 +- .../Traits/Version2x6/CommandsTrait.php | 2 +- .../Traits/Version2x8/CommandsTrait.php | 2 +- .../Traits/Version3x0/CommandsTrait.php | 2 +- .../Traits/Version3x2/CommandsTrait.php | 2 +- src/RedisClient/RedisClient.php | 5 -- .../Version2x6/ConnectionCommandsTest.php | 1 + tests/Unit/ClientFactoryTest.php | 62 ++++++++++++++++++ 20 files changed, 224 insertions(+), 54 deletions(-) create mode 100644 examples/raw_commands.php create mode 100644 src/RedisClient/ClientFactory.php create mode 100644 tests/Unit/ClientFactoryTest.php diff --git a/README.md b/README.md index 7ea279c..9c1d3e3 100644 --- a/README.md +++ b/README.md @@ -10,30 +10,48 @@ RedisClient is a fast, fully-functional and user-friendly client for Redis, opti - Support TCP/IP and UNIX sockets. - Support __PubSub__ and __monitor__ functionallity. - Support __pipeline__. +- Support RAW commands as strings `"SET foo bar"` or as arrays `['SET', 'foo', 'bar']`. - Connections to Redis are established lazily by the client upon the first command. - Easy to use with IDE, client has PHPDocs for all supported versions. +- By default, the client works with the latest stable version of Redis. ## Usage ### Create a new instance of RedisClient ```php 'tcp://127.0.0.1:6379', // or 'unix:///tmp/redis.sock' + 'timeout' => 2, + 'version' => '2.8.24' +]); + +echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL; +// RedisClient: 2.8 + +// Create new instance of client without factory + $Redis = new RedisClient([ 'server' => 'tcp://127.0.0.1:6379', // or 'unix:///tmp/redis.sock' 'timeout' => 2 ]); -echo 'RedisClient: '. $Redis->getVersion() . PHP_EOL; +echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL; echo 'Redis: '. $Redis->info('Server')['redis_version'] . PHP_EOL; +// By default, the client works with the latest stable version of Redis. // RedisClient: 3.0 // Redis: 3.0.3 + + ``` ### Example Please, see examples here: https://github.com/cheprasov/php-redis-client/tree/master/examples diff --git a/examples/create_new_instance.php b/examples/create_new_instance.php index ae4a061..b4c0fd8 100644 --- a/examples/create_new_instance.php +++ b/examples/create_new_instance.php @@ -15,35 +15,34 @@ namespace Examples; -require (dirname(__DIR__).'/src/autoloader.php'); -// or require ('vendor/autoload.php'); +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(); - -echo 'RedisClient: '. $Redis->getVersion() . PHP_EOL; +// 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; -// result: // RedisClient: 3.0 // 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:6379', // or 'unix:///tmp/redis.sock' 'timeout' => 2 ]); -echo 'RedisClient: '. $Redis->getVersion() . PHP_EOL; +echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL; echo 'Redis: '. $Redis->info('Server')['redis_version'] . PHP_EOL; - -// result: // RedisClient: 3.0 // Redis: 3.0.3 @@ -55,9 +54,16 @@ 'timeout' => 2 ]); -echo 'RedisClient: '. $Redis->getVersion() . PHP_EOL; -echo 'Redis: '. $Redis->info('Server')['redis_version'] . PHP_EOL; - -// result: +echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL; // RedisClient: 2.6 -// Redis: 3.0.3 + +// 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', // or 'unix:///tmp/redis.sock' + 'timeout' => 2, + 'version' => '2.8.24' +]); + +echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL; +// RedisClient: 2.8 diff --git a/examples/monitor.php b/examples/monitor.php index b12bcf0..89cff28 100644 --- a/examples/monitor.php +++ b/examples/monitor.php @@ -10,13 +10,13 @@ */ /** - * Using Monitor + * Monitor */ namespace Examples; -require (dirname(__DIR__).'/src/autoloader.php'); -// or require ('vendor/autoload.php'); +require (dirname(__DIR__).'/vendor/autoload.php'); +// or require (dirname(__DIR__).'/src/autoloader.php'); use RedisClient\RedisClient; diff --git a/examples/pipeline.php b/examples/pipeline.php index 3449b4a..ad65fa9 100644 --- a/examples/pipeline.php +++ b/examples/pipeline.php @@ -10,13 +10,13 @@ */ /** - * Using pipeline + * Pipeline */ namespace Examples; -require (dirname(__DIR__).'/src/autoloader.php'); -// or require ('vendor/autoload.php'); +require (dirname(__DIR__).'/vendor/autoload.php'); +// or require (dirname(__DIR__).'/src/autoloader.php'); use RedisClient\Pipeline\Pipeline; use RedisClient\Pipeline\PipelineInterface; diff --git a/examples/pubsub.php b/examples/pubsub.php index 5f5aeca..2d31adf 100644 --- a/examples/pubsub.php +++ b/examples/pubsub.php @@ -10,13 +10,13 @@ */ /** - * Using PubSub + * PubSub */ namespace Examples; -require (dirname(__DIR__).'/src/autoloader.php'); -// or require ('vendor/autoload.php'); +require (dirname(__DIR__).'/vendor/autoload.php'); +// or require (dirname(__DIR__).'/src/autoloader.php'); use RedisClient\RedisClient; diff --git a/examples/raw_commands.php b/examples/raw_commands.php new file mode 100644 index 0000000..4aae1c9 --- /dev/null +++ b/examples/raw_commands.php @@ -0,0 +1,41 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * RAW commands + */ + +namespace Examples; + +require (dirname(__DIR__).'/vendor/autoload.php'); +// or require (dirname(__DIR__).'/src/autoloader.php'); + +use RedisClient\RedisClient; + +$Redis = new RedisClient(); + +// Example 1. As string[] by +// Every part of command must be a separate string +// is better way to use raw commands than + +$Redis->executeRaw(['SET', 'foo', 'bar']); +echo 'result: '. $Redis->executeRaw(['GET', 'foo']) .PHP_EOL; +// bar + +// Example s. As string by +$Redis->executeRawString('SET foo bar'); +echo 'result: '. $Redis->executeRawString('GET foo') .PHP_EOL; +// bar + +// You can use quotes for keys and arguments +$Redis->executeRawString('SET "key with spaces" "or value with spaces"'); +echo 'result: '. $Redis->executeRawString('GET "key with spaces"') .PHP_EOL; +// or value with spaces diff --git a/examples/transactions.php b/examples/transactions.php index b539756..4a9b5a6 100644 --- a/examples/transactions.php +++ b/examples/transactions.php @@ -10,13 +10,13 @@ */ /** - * Using transactions + * Transactions */ namespace Examples; -require (dirname(__DIR__).'/src/autoloader.php'); -// or require ('vendor/autoload.php'); +require (dirname(__DIR__).'/vendor/autoload.php'); +// or require (dirname(__DIR__).'/src/autoloader.php'); use RedisClient\Pipeline\Pipeline; use RedisClient\Pipeline\PipelineInterface; diff --git a/src/RedisClient/Client/Version/RedisClient2x6.php b/src/RedisClient/Client/Version/RedisClient2x6.php index 7cb2b39..47e8bce 100644 --- a/src/RedisClient/Client/Version/RedisClient2x6.php +++ b/src/RedisClient/Client/Version/RedisClient2x6.php @@ -15,10 +15,6 @@ use RedisClient\Pipeline\PipelineInterface; use RedisClient\Pipeline\Version\Pipeline2x6; -/** - * Class RedisClient - * @package RedisClient - */ class RedisClient2x6 extends AbstractRedisClient { use CommandsTrait; diff --git a/src/RedisClient/Client/Version/RedisClient2x8.php b/src/RedisClient/Client/Version/RedisClient2x8.php index 5e4b618..a12d324 100644 --- a/src/RedisClient/Client/Version/RedisClient2x8.php +++ b/src/RedisClient/Client/Version/RedisClient2x8.php @@ -15,10 +15,6 @@ use RedisClient\Pipeline\PipelineInterface; use RedisClient\Pipeline\Version\Pipeline2x8; -/** - * Class RedisClient - * @package RedisClient - */ class RedisClient2x8 extends AbstractRedisClient { use CommandsTrait; diff --git a/src/RedisClient/Client/Version/RedisClient3x0.php b/src/RedisClient/Client/Version/RedisClient3x0.php index 0df6af4..6a1db1c 100644 --- a/src/RedisClient/Client/Version/RedisClient3x0.php +++ b/src/RedisClient/Client/Version/RedisClient3x0.php @@ -15,10 +15,6 @@ use RedisClient\Pipeline\PipelineInterface; use RedisClient\Pipeline\Version\Pipeline3x0; -/** - * Class RedisClient - * @package RedisClient - */ class RedisClient3x0 extends AbstractRedisClient { use CommandsTrait; diff --git a/src/RedisClient/Client/Version/RedisClient3x2.php b/src/RedisClient/Client/Version/RedisClient3x2.php index 67d04aa..d9d21dc 100644 --- a/src/RedisClient/Client/Version/RedisClient3x2.php +++ b/src/RedisClient/Client/Version/RedisClient3x2.php @@ -15,10 +15,6 @@ use RedisClient\Pipeline\PipelineInterface; use RedisClient\Pipeline\Version\Pipeline3x2; -/** - * Class RedisClient - * @package RedisClient - */ class RedisClient3x2 extends AbstractRedisClient { use CommandsTrait; diff --git a/src/RedisClient/ClientFactory.php b/src/RedisClient/ClientFactory.php new file mode 100644 index 0000000..711a1ac --- /dev/null +++ b/src/RedisClient/ClientFactory.php @@ -0,0 +1,63 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace RedisClient; + +use RedisClient\Client\AbstractRedisClient; +use RedisClient\Client\Version\RedisClient2x6; +use RedisClient\Client\Version\RedisClient2x8; +use RedisClient\Client\Version\RedisClient3x0; +use RedisClient\Client\Version\RedisClient3x2; + +class ClientFactory { + + protected static $versions = [ + '2.6' => RedisClient2x6::class, + '2.8' => RedisClient2x8::class, + '3.0' => RedisClient3x0::class, + '3.2' => RedisClient3x2::class, + ]; + + /** + * @param null|array $config + * @return RedisClient2x6|RedisClient2x8|RedisClient3x0|RedisClient3x2|RedisClient + */ + public static function create($config = null) { + if (isset($config['version'])) { + return self::createClientByVersion($config['version'], $config); + } + return new RedisClient($config); + } + + /** + * @param string $version + * @param null|array $config + * @return RedisClient2x6|RedisClient2x8|RedisClient3x0|RedisClient3x2 + */ + public static function createClientByVersion($version, $config = null) { + list($major, $minor, $patch) = explode('.', $version.'.0.0'); + $ver = (int) $major .'.'. (int) $minor; + + $versions = array_keys(self::$versions); + foreach ($versions as $v) { + if ($v >= $ver) { + $class = self::$versions[$v]; + break; + } + } + if (empty($class)) { + throw new \InvalidArgumentException( + 'RedisClient does not support Redis version '.$version.'. Please, use version ' .end($versions) + ); + } + return new $class($config); + } + +} diff --git a/src/RedisClient/Command/Traits/AbstractCommandsTrait.php b/src/RedisClient/Command/Traits/AbstractCommandsTrait.php index 0ea2c3f..cc31083 100644 --- a/src/RedisClient/Command/Traits/AbstractCommandsTrait.php +++ b/src/RedisClient/Command/Traits/AbstractCommandsTrait.php @@ -35,7 +35,7 @@ abstract protected function subscribeCommand(array $subCommand, array $unsubComm /** * @return string */ - abstract public function getVersion(); + abstract public function getSupportedVersion(); /** * @var array diff --git a/src/RedisClient/Command/Traits/Version2x6/CommandsTrait.php b/src/RedisClient/Command/Traits/Version2x6/CommandsTrait.php index 0af2be9..b4e07cf 100644 --- a/src/RedisClient/Command/Traits/Version2x6/CommandsTrait.php +++ b/src/RedisClient/Command/Traits/Version2x6/CommandsTrait.php @@ -31,7 +31,7 @@ trait CommandsTrait { /** * @return string */ - public function getVersion() { + public function getSupportedVersion() { return '2.6'; } diff --git a/src/RedisClient/Command/Traits/Version2x8/CommandsTrait.php b/src/RedisClient/Command/Traits/Version2x8/CommandsTrait.php index 25849d4..e1e3aa1 100644 --- a/src/RedisClient/Command/Traits/Version2x8/CommandsTrait.php +++ b/src/RedisClient/Command/Traits/Version2x8/CommandsTrait.php @@ -37,7 +37,7 @@ trait CommandsTrait { /** * @return string */ - public function getVersion() { + public function getSupportedVersion() { return '2.8'; } diff --git a/src/RedisClient/Command/Traits/Version3x0/CommandsTrait.php b/src/RedisClient/Command/Traits/Version3x0/CommandsTrait.php index c329a3f..c629c98 100644 --- a/src/RedisClient/Command/Traits/Version3x0/CommandsTrait.php +++ b/src/RedisClient/Command/Traits/Version3x0/CommandsTrait.php @@ -45,7 +45,7 @@ trait CommandsTrait { /** * @return string */ - public function getVersion() { + public function getSupportedVersion() { return '3.0'; } diff --git a/src/RedisClient/Command/Traits/Version3x2/CommandsTrait.php b/src/RedisClient/Command/Traits/Version3x2/CommandsTrait.php index c09e3bd..3a4de98 100644 --- a/src/RedisClient/Command/Traits/Version3x2/CommandsTrait.php +++ b/src/RedisClient/Command/Traits/Version3x2/CommandsTrait.php @@ -47,7 +47,7 @@ trait CommandsTrait { /** * @return string */ - public function getVersion() { + public function getSupportedVersion() { return '3.2'; } diff --git a/src/RedisClient/RedisClient.php b/src/RedisClient/RedisClient.php index e080a0b..d62a46d 100644 --- a/src/RedisClient/RedisClient.php +++ b/src/RedisClient/RedisClient.php @@ -13,11 +13,6 @@ use RedisClient\Pipeline\Pipeline; use RedisClient\Pipeline\PipelineInterface; - -/** - * Class RedisClient - * @package RedisClient - */ class RedisClient extends RedisClientLastStableVersion { /** diff --git a/tests/Integration/Version2x6/ConnectionCommandsTest.php b/tests/Integration/Version2x6/ConnectionCommandsTest.php index 2e8ea23..2c37f6a 100644 --- a/tests/Integration/Version2x6/ConnectionCommandsTest.php +++ b/tests/Integration/Version2x6/ConnectionCommandsTest.php @@ -11,6 +11,7 @@ namespace Test\Integration\Version2x6; use RedisClient\Client\Version\RedisClient2x6; +use RedisClient\ClientFactory; use RedisClient\Exception\ErrorResponseException; /** diff --git a/tests/Unit/ClientFactoryTest.php b/tests/Unit/ClientFactoryTest.php new file mode 100644 index 0000000..2fea86e --- /dev/null +++ b/tests/Unit/ClientFactoryTest.php @@ -0,0 +1,62 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Test\Unit; +use RedisClient\Client\Version\RedisClient2x6; +use RedisClient\Client\Version\RedisClient2x8; +use RedisClient\Client\Version\RedisClient3x0; +use RedisClient\Client\Version\RedisClient3x2; +use RedisClient\ClientFactory; + +/** + * @see ClientFactory + */ +class ClientFactoryTest extends \PHPUnit_Framework_TestCase { + + /** + * @see RedisProtocol::createClientByVersion + */ + public function test_createClientByVersion() { + $this->assertInstanceOf(RedisClient3x2::class, ClientFactory::createClientByVersion('3.2')); + $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(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)); + } +}