-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbasic_example.php
44 lines (35 loc) · 1.07 KB
/
basic_example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Dazzle\Loop\Model\SelectLoop;
use Dazzle\Loop\Loop;
use Dazzle\Redis\Redis;
$loop = new Loop(new SelectLoop());
//create a new redis client
$endpoint = @$argv[1]?:'tcp://127.0.0.1:6379';
$redis = new Redis($endpoint, $loop);
//send piped requests when redis client start
$redis->on('start', function(Redis $redis) {
$redis->flushDb();
$redis->set('test','Hello Dazzle Redis!')->then(function ($value) {
printf('result is %s'.PHP_EOL, $value);
});
$redis->get('test')->then(function ($value) {
printf('result is %s'.PHP_EOL, $value);
});
$redis->end();
});
//redis client stop handler
$redis->on('stop', function() use ($loop) {
echo 'stop right now'.PHP_EOL;
$loop->stop();
});
//redis client error handler
$redis->on('error', function(Exception $ex) {
printf('error message is %s'.PHP_EOL, $ex->getMessage());
});
//set redis client run when global loop start
$loop->onStart(function () use ($redis) {
$redis->start();
});
//then the global loop run...
$loop->start();