|
4 | 4 |
|
5 | 5 | ## 安装 |
6 | 6 |
|
| 7 | +通过 Composer 安装使用: |
| 8 | + |
7 | 9 | ```shell |
8 | 10 | composer require mixstart/delayer-client-php:1.* |
9 | 11 | ``` |
10 | 12 |
|
11 | 13 | ## DEMO |
12 | 14 |
|
13 | | -### `push` |
| 15 | +### `push` 方法 |
14 | 16 |
|
15 | | -[client_push_test.php](https://github.com/mixstart/delayer-client-php/blob/master/tests/client_push_test.php) |
| 17 | +放入一个任务。 |
16 | 18 |
|
17 | | -### `pop` |
| 19 | +```php |
| 20 | +<?php |
| 21 | +include '../vendor/autoload.php'; |
| 22 | +// 要与Delayer服务器端配置的redis的信息相同 |
| 23 | +$config = [ |
| 24 | + 'host' => '127.0.0.1', |
| 25 | + 'port' => 6379, |
| 26 | + 'database' => 0, |
| 27 | + 'password' => '', |
| 28 | +]; |
| 29 | +$client = new \Delayer\Client($config); |
| 30 | +// 任务数据,用户自己定义 |
| 31 | +$data = [ |
| 32 | + 'orderID' => '2018101712578956648885474', |
| 33 | + 'action' => 'close', |
| 34 | +]; |
| 35 | +$message = new \Delayer\Message([ |
| 36 | + // 任务ID,必须全局唯一 |
| 37 | + 'id' => md5(uniqid(mt_rand(), true)), |
| 38 | + // 主题,取出任务时需使用 |
| 39 | + 'topic' => 'close_order', |
| 40 | + // 必须转换为string类型 |
| 41 | + 'body' => json_encode($data), |
| 42 | +]); |
| 43 | +$ret = $client->push($message, 20, 604800); |
| 44 | +var_dump($ret); |
| 45 | +``` |
18 | 46 |
|
19 | | -[client_pop_test.php](https://github.com/mixstart/delayer-client-php/blob/master/tests/client_pop_test.php) |
| 47 | +### `pop` 方法 |
20 | 48 |
|
21 | | -### `bPop` |
| 49 | +取出一个到期的任务。 |
22 | 50 |
|
23 | | -[client_bpop_test.php](https://github.com/mixstart/delayer-client-php/blob/master/tests/client_bpop_test.php) |
| 51 | +```php |
| 52 | +<?php |
| 53 | +include '../vendor/autoload.php'; |
| 54 | +// 要与Delayer服务器端配置的redis的信息相同 |
| 55 | +$config = [ |
| 56 | + 'host' => '127.0.0.1', |
| 57 | + 'port' => 6379, |
| 58 | + 'database' => 0, |
| 59 | + 'password' => '', |
| 60 | +]; |
| 61 | +$client = new \Delayer\Client($config); |
| 62 | +$message = $client->pop('close_order'); |
| 63 | +// 没有任务时,返回false |
| 64 | +var_dump($message); |
| 65 | +var_dump($message->body); |
| 66 | +``` |
24 | 67 |
|
25 | | -### `remove` |
| 68 | +### `bPop` 方法 |
26 | 69 |
|
27 | | -[client_remove_test.php](https://github.com/mixstart/delayer-client-php/blob/master/tests/client_remove_test.php) |
| 70 | +阻塞取出一个到期的任务。 |
| 71 | + |
| 72 | +```php |
| 73 | +<?php |
| 74 | +include '../vendor/autoload.php'; |
| 75 | +// 要与Delayer服务器端配置的redis的信息相同 |
| 76 | +$config = [ |
| 77 | + 'host' => '127.0.0.1', |
| 78 | + 'port' => 6379, |
| 79 | + 'database' => 0, |
| 80 | + 'password' => '', |
| 81 | +]; |
| 82 | +$client = new \Delayer\Client($config); |
| 83 | +$message = $client->bPop('close_order', 10); |
| 84 | +// 没有任务时,返回false |
| 85 | +var_dump($message); |
| 86 | +var_dump($message->body); |
| 87 | +``` |
| 88 | + |
| 89 | +### `remove` 方法 |
| 90 | + |
| 91 | +移除一个未到期的任务。 |
| 92 | + |
| 93 | +```php |
| 94 | +<?php |
| 95 | +include '../vendor/autoload.php'; |
| 96 | +// 要与Delayer服务器端配置的redis的信息相同 |
| 97 | +$config = [ |
| 98 | + 'host' => '127.0.0.1', |
| 99 | + 'port' => 6379, |
| 100 | + 'database' => 0, |
| 101 | + 'password' => '', |
| 102 | +]; |
| 103 | +$client = new \Delayer\Client($config); |
| 104 | +// push时定义的任务ID |
| 105 | +$id = '***'; |
| 106 | +$ret = $client->remove($id); |
| 107 | +var_dump($ret); |
| 108 | +``` |
28 | 109 |
|
29 | 110 | ## License |
30 | 111 |
|
|
0 commit comments