Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php
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;

// 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

// 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
Expand Down
32 changes: 19 additions & 13 deletions examples/create_new_instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
6 changes: 3 additions & 3 deletions examples/monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions examples/pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions examples/pubsub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
41 changes: 41 additions & 0 deletions examples/raw_commands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* This file is part of RedisClient.
* git: https://github.com/cheprasov/php-redis-client
*
* (C) Alexander Cheprasov <cheprasov.84@ya.ru>
*
* 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 <executeRaw>
// Every part of command must be a separate string
// <executeRaw> is better way to use raw commands than <executeRawString>

$Redis->executeRaw(['SET', 'foo', 'bar']);
echo 'result: '. $Redis->executeRaw(['GET', 'foo']) .PHP_EOL;
// bar

// Example s. As string by <executeRawString>
$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
6 changes: 3 additions & 3 deletions examples/transactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions src/RedisClient/Client/Version/RedisClient2x6.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
use RedisClient\Pipeline\PipelineInterface;
use RedisClient\Pipeline\Version\Pipeline2x6;

/**
* Class RedisClient
* @package RedisClient
*/
class RedisClient2x6 extends AbstractRedisClient {
use CommandsTrait;

Expand Down
4 changes: 0 additions & 4 deletions src/RedisClient/Client/Version/RedisClient2x8.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
use RedisClient\Pipeline\PipelineInterface;
use RedisClient\Pipeline\Version\Pipeline2x8;

/**
* Class RedisClient
* @package RedisClient
*/
class RedisClient2x8 extends AbstractRedisClient {
use CommandsTrait;

Expand Down
4 changes: 0 additions & 4 deletions src/RedisClient/Client/Version/RedisClient3x0.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
use RedisClient\Pipeline\PipelineInterface;
use RedisClient\Pipeline\Version\Pipeline3x0;

/**
* Class RedisClient
* @package RedisClient
*/
class RedisClient3x0 extends AbstractRedisClient {
use CommandsTrait;

Expand Down
4 changes: 0 additions & 4 deletions src/RedisClient/Client/Version/RedisClient3x2.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
use RedisClient\Pipeline\PipelineInterface;
use RedisClient\Pipeline\Version\Pipeline3x2;

/**
* Class RedisClient
* @package RedisClient
*/
class RedisClient3x2 extends AbstractRedisClient {
use CommandsTrait;

Expand Down
63 changes: 63 additions & 0 deletions src/RedisClient/ClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* This file is part of RedisClient.
* git: https://github.com/cheprasov/php-redis-client
*
* (C) Alexander Cheprasov <cheprasov.84@ya.ru>
*
* 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);
}

}
2 changes: 1 addition & 1 deletion src/RedisClient/Command/Traits/AbstractCommandsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract protected function subscribeCommand(array $subCommand, array $unsubComm
/**
* @return string
*/
abstract public function getVersion();
abstract public function getSupportedVersion();

/**
* @var array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ trait CommandsTrait {
/**
* @return string
*/
public function getVersion() {
public function getSupportedVersion() {
return '2.6';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ trait CommandsTrait {
/**
* @return string
*/
public function getVersion() {
public function getSupportedVersion() {
return '2.8';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ trait CommandsTrait {
/**
* @return string
*/
public function getVersion() {
public function getSupportedVersion() {
return '3.0';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ trait CommandsTrait {
/**
* @return string
*/
public function getVersion() {
public function getSupportedVersion() {
return '3.2';
}

Expand Down
5 changes: 0 additions & 5 deletions src/RedisClient/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
use RedisClient\Pipeline\Pipeline;
use RedisClient\Pipeline\PipelineInterface;


/**
* Class RedisClient
* @package RedisClient
*/
class RedisClient extends RedisClientLastStableVersion {

/**
Expand Down
1 change: 1 addition & 0 deletions tests/Integration/Version2x6/ConnectionCommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Test\Integration\Version2x6;

use RedisClient\Client\Version\RedisClient2x6;
use RedisClient\ClientFactory;
use RedisClient\Exception\ErrorResponseException;

/**
Expand Down
Loading