Skip to content

Commit bc74797

Browse files
authored
Merge pull request #43 from cheprasov/v1.4.0
v1.4.0
2 parents 00eb0b7 + 51c65ff commit bc74797

16 files changed

+241
-64
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## CHANGELOG
22

3+
### v1.4.0 (2016-07-18)
4+
- You can choose default version of Redis Client (**ClientFactory::setDefaultRedisVersion**).
5+
- Added parameter 'password' for config.
6+
- Added parameter 'database' for config.
7+
38
### v1.3.1 (2016-05-18)
49
- By default, the client works with the latest stable version of Redis (3.2.0).
510

README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
22
[![Latest Stable Version](https://poser.pugx.org/cheprasov/php-redis-client/v/stable)](https://packagist.org/packages/cheprasov/php-redis-client)
33
[![Total Downloads](https://poser.pugx.org/cheprasov/php-redis-client/downloads)](https://packagist.org/packages/cheprasov/php-redis-client)
4-
# RedisClient v1.3.1 for PHP >= 5.5
4+
# RedisClient v1.4.0 for PHP >= 5.5
55

66
## About
77
RedisClient is a fast, fully-functional and user-friendly client for Redis, optimized for performance. RedisClient supports the latest versions of Redis starting from __2.6__ to __3.2__
@@ -19,6 +19,27 @@ RedisClient is a fast, fully-functional and user-friendly client for Redis, opti
1919

2020
## Usage
2121

22+
### Config
23+
24+
```
25+
$Redis = ClientFactory::create([
26+
// Optional. Default = 'tcp://127.0.0.1:6379'. You can use 'unix:///tmp/redis.sock'
27+
'server' => 'tcp://127.0.0.1:6379',
28+
29+
// Optional. Default = 1
30+
'timeout' => 2,
31+
32+
// Optional. Specify version to avoid some unexpected errors.
33+
'version' => '2.8.24',
34+
35+
// Optional. Use it only if Redis server requires password (AUTH)
36+
'password' => 'some-password',
37+
38+
// Use it, if you want to select not default db (db != 0) on connect
39+
'database' => 1,
40+
]);
41+
```
42+
2243
### Create a new instance of RedisClient
2344
```php
2445
<?php

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cheprasov/php-redis-client",
3-
"version": "1.3.1",
3+
"version": "1.4.0",
44
"description": "Php client for Redis. It is a fast, fully-functional and user-friendly client for Redis, optimized for performance. RedisClient supports the latest versions of Redis starting from 2.6 to 3.2.0",
55
"homepage": "http://github.com/cheprasov/php-redis-client",
66
"minimum-stability": "stable",

phpunit.xml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<const name="TEST_REDIS_SERVER_3x0_2" value="tcp://127.0.0.1:6386" />
3535
<const name="TEST_REDIS_SERVER_3x2_1" value="tcp://127.0.0.1:6387" />
3636
<const name="TEST_REDIS_SERVER_3x2_2" value="tcp://127.0.0.1:6388" />
37+
<const name="TEST_REDIS_SERVER_PASSWORD" value="test-password-123" />
3738
</php>
3839

3940
</phpunit>

src/RedisClient/Client/AbstractRedisClient.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
use RedisClient\Pipeline\PipelineInterface;
1818
use RedisClient\Protocol\ProtocolInterface;
1919
use RedisClient\Protocol\RedisProtocol;
20+
use RedisClient\RedisClient;
2021

2122
abstract class AbstractRedisClient {
2223

23-
const VERSION = '1.3.1';
24+
const VERSION = '1.4.0';
2425

2526
const CONFIG_SERVER = 'server';
2627
const CONFIG_TIMEOUT = 'timeout';
28+
const CONFIG_DATABASE = 'database';
29+
const CONFIG_PASSWORD = 'password';
2730

2831
/**
2932
* Default configuration
@@ -59,7 +62,7 @@ protected function getConfig($param = null) {
5962
if (!$param) {
6063
return $this->config;
6164
}
62-
return empty($this->config[$param]) ? null : $this->config[$param];
65+
return isset($this->config[$param]) ? $this->config[$param] : null;
6366
}
6467

6568
/**
@@ -73,10 +76,21 @@ protected function getProtocol() {
7376
$this->getConfig(self::CONFIG_TIMEOUT)
7477
)
7578
);
79+
$this->onProtocolInit();
7680
}
7781
return $this->Protocol;
7882
}
7983

84+
protected function onProtocolInit() {
85+
/** @var RedisClient $this */
86+
if ($password = $this->getConfig(self::CONFIG_PASSWORD)) {
87+
$this->auth($password);
88+
}
89+
if ($db = (int) $this->getConfig(self::CONFIG_DATABASE)) {
90+
$this->select($db);
91+
}
92+
}
93+
8094
/**
8195
* @inheritdoc
8296
*/

src/RedisClient/ClientFactory.php

+49-4
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,58 @@
1414
use RedisClient\Client\Version\RedisClient2x8;
1515
use RedisClient\Client\Version\RedisClient3x0;
1616
use RedisClient\Client\Version\RedisClient3x2;
17+
use RedisClient\Exception\ErrorException;
18+
use RedisClient\Exception\InvalidArgumentException;
1719

1820
class ClientFactory {
1921

22+
const REDIS_VERSION_2x6 = '2.6';
23+
const REDIS_VERSION_2x8 = '2.8';
24+
const REDIS_VERSION_3x0 = '3.0';
25+
const REDIS_VERSION_3x2 = '3.2';
26+
27+
/**
28+
* @var string|null
29+
*/
30+
protected static $defaultRedisVersion;
31+
32+
/**
33+
* @var array
34+
*/
2035
protected static $versions = [
21-
'2.6' => RedisClient2x6::class,
22-
'2.8' => RedisClient2x8::class,
23-
'3.0' => RedisClient3x0::class,
24-
'3.2' => RedisClient3x2::class,
36+
self::REDIS_VERSION_2x6 => RedisClient2x6::class,
37+
self::REDIS_VERSION_2x8 => RedisClient2x8::class,
38+
self::REDIS_VERSION_3x0 => RedisClient3x0::class,
39+
self::REDIS_VERSION_3x2 => RedisClient3x2::class,
2540
];
2641

42+
/**
43+
* @return string
44+
*/
45+
public static function getDefaultRedisVersion() {
46+
return self::$defaultRedisVersion ?: self::REDIS_VERSION_3x2;
47+
}
48+
49+
/**
50+
* @param string $version
51+
* @throws InvalidArgumentException
52+
* @throws ErrorException
53+
*/
54+
public static function setDefaultRedisVersion($version) {
55+
if (class_exists('\RedisClient\RedisClient', false)) {
56+
throw new ErrorException('You can setup default version only if class "\RedisClient\RedisClient" is not loaded.');
57+
}
58+
if (self::$defaultRedisVersion) {
59+
throw new ErrorException('Default Version is defined already.');
60+
}
61+
if (!array_key_exists($version, self::$versions)) {
62+
throw new InvalidArgumentException(
63+
'Invalid version. Supported versions are '. implode(',', array_keys(self::$versions))
64+
);
65+
}
66+
self::$defaultRedisVersion = $version;
67+
}
68+
2769
/**
2870
* @param null|array $config
2971
* @return RedisClient2x6|RedisClient2x8|RedisClient3x0|RedisClient3x2|RedisClient
@@ -48,6 +90,9 @@ public static function createClientByVersion($version, $config = null) {
4890
foreach ($versions as $v) {
4991
if ($v >= $ver) {
5092
$class = self::$versions[$v];
93+
if (!self::$defaultRedisVersion) {
94+
self::setDefaultRedisVersion($v);
95+
}
5196
break;
5297
}
5398
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* This file is part of RedisClient.
4+
* git: https://github.com/cheprasov/php-redis-client
5+
*
6+
* (C) Alexander Cheprasov <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace RedisClient\Exception;
12+
13+
class InvalidArgumentException extends RedisException {
14+
15+
}

src/RedisClient/Pipeline/Pipeline.php

+19-5
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,24 @@
1010
*/
1111
namespace RedisClient\Pipeline;
1212

13-
use RedisClient\Pipeline\Version\Pipeline3x2 as PipelineLastStableVersion;
13+
use RedisClient\ClientFactory;
14+
use RedisClient\Pipeline\Version\Pipeline2x6;
15+
use RedisClient\Pipeline\Version\Pipeline2x8;
16+
use RedisClient\Pipeline\Version\Pipeline3x0;
17+
use RedisClient\Pipeline\Version\Pipeline3x2;
1418

15-
/**
16-
* @inheritdoc
17-
*/
18-
class Pipeline extends PipelineLastStableVersion {
19+
switch (ClientFactory::getDefaultRedisVersion()) {
20+
case '2.6':
21+
class Pipeline extends Pipeline2x6 {};
22+
break;
23+
case '2.8':
24+
class Pipeline extends Pipeline2x8 {};
25+
break;
26+
case '3.0':
27+
class Pipeline extends Pipeline3x0 {};
28+
break;
29+
case '3.2':
30+
default:
31+
class Pipeline extends Pipeline3x2 {};
32+
break;
1933
}

src/RedisClient/RedisClient.php

+18-12
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,24 @@
99
* file that was distributed with this source code.
1010
*/
1111
namespace RedisClient;
12-
use RedisClient\Client\Version\RedisClient3x2 as RedisClientLastStableVersion;
13-
use RedisClient\Pipeline\Pipeline;
14-
use RedisClient\Pipeline\PipelineInterface;
1512

16-
class RedisClient extends RedisClientLastStableVersion {
17-
18-
/**
19-
* @param \Closure|null $Pipeline
20-
* @return PipelineInterface
21-
*/
22-
protected function createPipeline(\Closure $Pipeline = null) {
23-
return new Pipeline($Pipeline);
24-
}
13+
use RedisClient\Client\Version\RedisClient2x6;
14+
use RedisClient\Client\Version\RedisClient2x8;
15+
use RedisClient\Client\Version\RedisClient3x0;
16+
use RedisClient\Client\Version\RedisClient3x2;
2517

18+
switch (ClientFactory::getDefaultRedisVersion()) {
19+
case '2.6':
20+
class RedisClient extends RedisClient2x6 {};
21+
break;
22+
case '2.8':
23+
class RedisClient extends RedisClient2x8 {};
24+
break;
25+
case '3.0':
26+
class RedisClient extends RedisClient3x0 {};
27+
break;
28+
case '3.2':
29+
default:
30+
class RedisClient extends RedisClient3x2 {};
31+
break;
2632
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* This file is part of RedisClient.
4+
* git: https://github.com/cheprasov/php-redis-client
5+
*
6+
* (C) Alexander Cheprasov <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace Test\Integration;
12+
13+
use RedisClient\Client\Version\RedisClient2x6;
14+
15+
/**
16+
* Check Redis Versions
17+
*/
18+
class DefaultDatabaseTest extends \PHPUnit_Framework_TestCase {
19+
20+
public function test_defaultDatabase() {
21+
$Redis = new RedisClient2x6([
22+
'server' => TEST_REDIS_SERVER_2x6_1,
23+
]);
24+
$Redis->flushall();
25+
26+
for ($i = 0; $i <= 7; ++$i) {
27+
$this->assertTrue($Redis->select($i));
28+
$this->assertTrue($Redis->set('db', $i));
29+
}
30+
31+
for ($i = 0; $i <= 7; ++$i) {
32+
$Redis = new RedisClient2x6([
33+
'server' => TEST_REDIS_SERVER_2x6_1,
34+
'database' => $i
35+
]);
36+
$this->assertEquals($i, $Redis->get('db'));
37+
}
38+
}
39+
40+
}

tests/Integration/RedisVersionTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ class RedisVersionTest extends \PHPUnit_Framework_TestCase {
3333
*
3434
*/
3535
public function test_RedisVersions() {
36-
foreach ($this->versions as $arr) {
36+
foreach ($this->versions as $n => $arr) {
3737
list($server, $clientVersion, $serverVersion) = $arr;
3838

3939
$Redis = ClientFactory::create([
4040
'server' => $server,
4141
'timeout' => 2,
4242
'version' => $clientVersion,
43+
'password' => $n % 2 ? TEST_REDIS_SERVER_PASSWORD : null,
4344
]);
4445

4546
$this->assertSame($clientVersion, $Redis->getSupportedVersion());

tests/Integration/Version2x6/KeysCommandsTest.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static function setUpBeforeClass() {
4242
static::$Redis2 = new RedisClient2x6([
4343
'server' => static::TEST_REDIS_SERVER_2,
4444
'timeout' => 2,
45+
'password' => TEST_REDIS_SERVER_PASSWORD,
4546
]);
4647
}
4748

@@ -201,21 +202,22 @@ public function test_migrate() {
201202
$Redis = static::$Redis;
202203
$Redis2 = static::$Redis2;
203204

205+
$this->assertSame(true, $Redis->flushall());
204206
$this->assertSame(true, $Redis2->flushall());
205207

206-
list(, $host, $port) = explode(':', str_replace('/', '', static::TEST_REDIS_SERVER_2), 3);
208+
list(, $host, $port) = explode(':', str_replace('/', '', static::TEST_REDIS_SERVER_1), 3);
207209

208-
$this->assertSame(true, $Redis->set('one', 1));
210+
$this->assertSame(true, $Redis2->set('one', 1));
209211

210-
$this->assertSame(null, $Redis2->get('one'));
211-
$this->assertSame(true, $Redis->migrate($host, $port, 'one', 0, 100));
212-
$this->assertSame('1', $Redis2->get('one'));
213212
$this->assertSame(null, $Redis->get('one'));
213+
$this->assertSame(true, $Redis2->migrate($host, $port, 'one', 0, 100));
214+
$this->assertSame('1', $Redis->get('one'));
215+
$this->assertSame(null, $Redis2->get('one'));
214216

215-
$this->assertSame(true, $Redis->set('one', 11));
217+
$this->assertSame(true, $Redis2->set('one', 11));
216218

217219
try {
218-
$this->assertSame(true, $Redis->migrate($host, $port, 'one', 0, 100));
220+
$this->assertSame(true, $Redis2->migrate($host, $port, 'one', 0, 100));
219221
$this->assertFalse('Expect Exception');
220222
} catch (\Exception $Ex) {
221223
$this->assertInstanceOf(ErrorResponseException::class, $Ex);

tests/Integration/Version2x8/KeysCommandsTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public static function setUpBeforeClass() {
4949
static::$Redis2 = new RedisClient2x8([
5050
'server' => static::TEST_REDIS_SERVER_2,
5151
'timeout' => 2,
52+
'password' => TEST_REDIS_SERVER_PASSWORD,
5253
]);
5354
}
5455

0 commit comments

Comments
 (0)