ledc / redis
ledc/redis,提取自workerman的redis组件
v5.0.2
2023-12-18 14:58 UTC
Requires
- php: ^8.1|^8.2|^8.3
- ext-redis: *
- workerman/redis: ^2.0
- workerman/webman-framework: ^1.5.0
- workerman/workerman: ^4.1.0||^5.0.0
This package is auto-updated.
Last update: 2024-03-30 05:06:16 UTC
README
composer require ledc/redis
Usage
test.php
<?php require __DIR__ . '/vendor/autoload.php'; use Workerman\Worker; use Workerman\Lib\Timer; use Ledc\Redis\RedisQueueClient; $worker = new Worker(); $worker->onWorkerStart = function () { $client = new RedisQueueClient('redis://127.0.0.1:6379'); $client->subscribe('user-1', function($data){ echo "user-1\n"; var_export($data); }); $client->subscribe('user-2', function($data){ echo "user-2\n"; var_export($data); }); Timer::add(1, function()use($client){ $client->send('user-1', ['some', 'data']); }); }; Worker::runAll();
Run with command php test.php start
or php test.php start -d
.
API
__construct (string $address, [array $options])
Create an instance by $address and $options.
-
$address
for exampleredis://ip:6379
. -
$options
is the client connection options. Defaults:auth
: default ''db
: default 0retry_seconds
: Retry interval after consumption failuremax_attempts
: Maximum number of retries after consumption failure
send(String $queue, Mixed $data, [int $dely=0])
Send a message to a queue
$queue
is the queue to publish to,String
$data
is the message to publish,Mixed
$dely
is delay seconds for delayed consumption,Int
subscribe(mixed $queue, callable $callback)
Subscribe to a queue or queues
$queue
is aString
queue or anArray
which has as keys the queue name to subscribe.$callback
-function (Mixed $data)
,$data
is the data sent bysend($queue, $data)
.
unsubscribe(mixed $queue)
Unsubscribe from a queue or queues
$queue
is aString
queue or an array of queue to unsubscribe from