Skip to content

Commit 17fff95

Browse files
committed
Merge pull request #25 from cheprasov/dev-with-versions
Added examples, test for RedisProtocol and fixed phpdocs
2 parents 95f78c3 + f1168c0 commit 17fff95

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1473
-887
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"description": "Php client for Redis",
55
"homepage": "http://github.com/cheprasov/php-redis-client",
6-
"minimum-stability": "dev",
6+
"minimum-stability": "stable",
77
"license": "proprietary",
88
"authors": [
99
{
@@ -21,6 +21,6 @@
2121
},
2222
"require-dev": {
2323
"phpunit/phpunit": "5.1.*",
24-
"cheprasov/php-simple-profiler": "*"
24+
"cheprasov/php-simple-profiler": "master-dev"
2525
}
2626
}

examples/create_new_instance.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
12+
/**
13+
* Create a new instance of RedisClient
14+
*/
15+
16+
namespace Examples;
17+
18+
require (dirname(__DIR__).'/src/autoloader.php');
19+
20+
use RedisClient\RedisClient;
21+
use RedisClient\Client\Version\RedisClient2x6;
22+
23+
// Example 1. Create new Instance with default config
24+
25+
$Redis = new RedisClient();
26+
27+
echo 'RedisClient: '. $Redis->getVersion() . PHP_EOL;
28+
echo 'Redis: '. $Redis->info('Server')['redis_version'] . PHP_EOL;
29+
30+
// result:
31+
// RedisClient: 3.0
32+
// Redis: 3.0.3
33+
34+
35+
// Example 2. Create new Instance with config
36+
37+
$Redis = new RedisClient([
38+
'server' => 'tcp://127.0.0.1:6379',
39+
'timeout' => 2
40+
]);
41+
42+
echo 'RedisClient: '. $Redis->getVersion() . PHP_EOL;
43+
echo 'Redis: '. $Redis->info('Server')['redis_version'] . PHP_EOL;
44+
45+
// result:
46+
// RedisClient: 3.0
47+
// Redis: 3.0.3
48+
49+
50+
// Example 3. Create new Instance for Redis version 2.6.x with config
51+
52+
$Redis = new RedisClient2x6([
53+
'server' => 'tcp://127.0.0.1:6379',
54+
'timeout' => 2
55+
]);
56+
57+
echo 'RedisClient: '. $Redis->getVersion() . PHP_EOL;
58+
echo 'Redis: '. $Redis->info('Server')['redis_version'] . PHP_EOL;
59+
60+
// result:
61+
// RedisClient: 2.6
62+
// Redis: 3.0.3

examples/monitor.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
12+
/**
13+
* Using Monitor
14+
*/
15+
16+
namespace Examples;
17+
18+
require (dirname(__DIR__).'/src/autoloader.php');
19+
20+
use RedisClient\RedisClient;
21+
22+
// Example 1. monitor with timeout
23+
24+
$Redis = new RedisClient([
25+
'timeout' => 10 // for waiting answer for 10 seconds
26+
]);
27+
28+
$Redis->monitor(function($message) {
29+
// This function will be called on message and on timeout
30+
if (!isset($message)) {
31+
echo 'No any message for 10 second... exit'. PHP_EOL;
32+
// return <false> for stop monitoring and exit
33+
return false;
34+
}
35+
36+
echo $message, PHP_EOL;
37+
// return <true> for to wait next message
38+
return true;
39+
});
40+
41+
// NOTE! You can not use $Redis after monitor,
42+
// because connection with redis will be closed,
43+
// it is correct way to stop monitor.

examples/pipeline.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
12+
/**
13+
* Using pipeline
14+
*/
15+
16+
namespace Examples;
17+
18+
require (dirname(__DIR__).'/src/autoloader.php');
19+
20+
use RedisClient\Pipeline\Pipeline;
21+
use RedisClient\Pipeline\PipelineInterface;
22+
use RedisClient\RedisClient;
23+
24+
$Redis = new RedisClient();
25+
26+
// Example 1. via new Pipeline
27+
28+
// Method 'pipeline' without params returns new Pipeline object;
29+
$Pipeline = $Redis->pipeline();
30+
// or $Pipeline = new Pipeline();
31+
$Pipeline->set('foo', 'bar')->get('foo')->flushall();
32+
33+
var_dump($Redis->pipeline($Pipeline));
34+
35+
// result:
36+
// array(3) {
37+
// [0]=> bool(true)
38+
// [1]=> string(3) "bar"
39+
// [2]=> bool(true)
40+
// }
41+
42+
43+
// Example 2. via Closure
44+
45+
// Method 'pipeline' without params returns new Pipeline object;
46+
$result = $Redis->pipeline(
47+
function(PipelineInterface $Pipeline) {
48+
/** @var Pipeline $Pipeline */
49+
$Pipeline->set('foo', 'bar');
50+
$Pipeline->get('foo');
51+
$Pipeline->flushall();
52+
}
53+
);
54+
55+
var_dump($result);
56+
57+
// result:
58+
// array(3) {
59+
// [0]=> bool(true)
60+
// [1]=> string(3) "bar"
61+
// [2]=> bool(true)
62+
// }

examples/pubsub.php

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
12+
/**
13+
* Using PubSub
14+
*/
15+
16+
namespace Examples;
17+
18+
require (dirname(__DIR__).'/src/autoloader.php');
19+
20+
use RedisClient\RedisClient;
21+
22+
// Example 1. subscribe
23+
24+
$Redis = new RedisClient([
25+
'timeout' => 0 // for waiting answer infinitely
26+
]);
27+
28+
$Redis->subscribe('channel.name', function($type, $channel, $message) {
29+
// This function will be called on subscribe and on message
30+
if ($type === 'subscribe') {
31+
// Note, if $type === 'subscribe'
32+
// then $channel = <channel-name>
33+
// and $message = <count of subsribers>
34+
echo 'Subscribed to channel <', $channel, '>', PHP_EOL;
35+
} elseif ($type === 'message') {
36+
echo 'Message <', $message, '> from channel <', $channel, '>', PHP_EOL;
37+
if ($message === 'quit') {
38+
// return <false> for unsubscribe and exit
39+
return false;
40+
}
41+
}
42+
// return <true> for to wait next message
43+
return true;
44+
});
45+
46+
// Example 2. subscribe with timeout
47+
48+
$Redis = new RedisClient([
49+
'timeout' => 10 // for waiting answer for 10 seconds
50+
]);
51+
52+
$Redis->subscribe('channel.name', function($type, $channel, $message) {
53+
// This function will be called on subscribe, on message and on timeout
54+
if (!isset($type)) {
55+
echo 'No any message for 10 second... exit'. PHP_EOL;
56+
// return <false> for unsubscribe and exit
57+
return false;
58+
}
59+
// This function will be called on subscribe and on message
60+
if ($type === 'subscribe') {
61+
// Note, if $type === 'subscribe'
62+
// then $channel = <channel-name>
63+
// and $message = <count of subsribers>
64+
echo 'Subscribed to channel <', $channel, '>', PHP_EOL;
65+
} elseif ($type === 'message') {
66+
echo 'Message <', $message, '> from channel <', $channel, '>', PHP_EOL;
67+
}
68+
// return <true> for to wait next message
69+
return true;
70+
});
71+
72+
// Example 3. subscribe with external timeout
73+
74+
$Redis = new RedisClient([
75+
'timeout' => 0.25 // for waiting answer for 0.25 seconds
76+
]);
77+
78+
$time = microtime(true);
79+
$Redis->subscribe(['channel.name1', 'channel.name2'], function($type, $channel, $message) use ($time) {
80+
// This function will be called on subscribe, on message and on timeout (every 0.25 seconds)
81+
if (!isset($type)) {
82+
echo 'No any message for 0.25 seconds...'. PHP_EOL;
83+
84+
// Will unsubscribe and exit after 10 seconds of timeout
85+
if (microtime(true) - $time > 10) {
86+
// return <false> for unsubscribe and exit
87+
return false;
88+
}
89+
}
90+
// This function will be called on subscribe and on message
91+
if ($type === 'subscribe') {
92+
// Note, if $type === 'subscribe'
93+
// then $channel = <channel-name>
94+
// and $message = <count of subsribers>
95+
echo 'Subscribed to channel <', $channel, '>', PHP_EOL;
96+
} elseif ($type === 'message') {
97+
echo 'Message <', $message, '> from channel <', $channel, '>', PHP_EOL;
98+
}
99+
// return <true> for to wait next message
100+
return true;
101+
});
102+
103+
104+
// Example 4. psubscribe
105+
106+
$Redis = new RedisClient([
107+
'timeout' => 0 // for waiting answer infinitely
108+
]);
109+
110+
$Redis->psubscribe('channel.*', function($type, $pattern, $channel, $message) {
111+
// This function will be called on subscribe and on message
112+
if ($type === 'psubscribe') {
113+
// Note, if $type === 'psubscribe'
114+
// then $pattern = <channel-name>
115+
// and $channel = <count of subsribers>
116+
echo 'Subscribed to channel <', $pattern, '>', PHP_EOL;
117+
} elseif ($type === 'pmessage') {
118+
echo 'Message <', $message, '> from channel <', $channel, '> by pattern <', $pattern, '>', PHP_EOL;
119+
if ($message === 'quit') {
120+
// return <false> for unsubscribe and exit
121+
return false;
122+
}
123+
}
124+
// return <true> for to wait next message
125+
return true;
126+
});

examples/transactions.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
12+
/**
13+
* Using transactions
14+
*/
15+
16+
namespace Examples;
17+
18+
require (dirname(__DIR__).'/src/autoloader.php');
19+
20+
use RedisClient\Pipeline\Pipeline;
21+
use RedisClient\Pipeline\PipelineInterface;
22+
use RedisClient\RedisClient;
23+
24+
$Redis = new RedisClient();
25+
26+
// Example 1. Transactions
27+
28+
$Redis->watch(['foo', 'bar']);;
29+
$Redis->multi();
30+
$Redis->set('foo', 'bar');
31+
$Redis->set('bar', 'foo');
32+
$result = $Redis->exec();
33+
34+
var_dump($result);
35+
36+
// array(2) {
37+
// [0]=> bool(true)
38+
// [1]=> bool(true)
39+
// }
40+
41+
// Example 2. True way to use transactions via Pipeline
42+
43+
$result = $Redis->pipeline(function(PipelineInterface $Pipeline) {
44+
/** @var Pipeline $Pipeline */
45+
$Pipeline->watch(['foo', 'bar']);;
46+
$Pipeline->multi();
47+
$Pipeline->set('foo', 'bar');
48+
$Pipeline->set('bar', 'foo');
49+
$Pipeline->exec();
50+
});
51+
52+
var_dump(end($result));
53+
54+
// array(2) {
55+
// [0]=> bool(true)
56+
// [1]=> bool(true)
57+
// }

src/RedisClient/Command/Traits/Version2x6/HashesCommandsTrait.php

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ public function hmget($key, $fields) {
153153
* Time complexity: O(N) where N is the number of fields being set.
154154
* @link http://redis.io/commands/hmset
155155
*
156+
* @param string $key
157+
* @param string|string[] $fieldValues
156158
* @return bool True
157159
*/
158160
public function hmset($key, array $fieldValues) {

0 commit comments

Comments
 (0)