hollisho/yii2-redis-pub-sub

Yii2 Redis pub/sub(Publish,Subscribe)

0.1.0 2018-11-22 10:56 UTC

This package is auto-updated.

Last update: 2024-11-23 00:00:31 UTC


README

基于yii2通过redis的订阅/发布者模式实现的消息队列

install

  • 安装phpredis扩展
  • 安装代码
composer require --prefer-dist hollisho/yii2-redis-pub-sub

usage

  • 配置文件
'redisPubSub'=>[
    'class' => 'hollisho\redis_pub_sub\RedisPubSub',
    'connect' => [
        'class' => 'yii\redis\Connection',
        'hostname' => '127.0.0.1',
        'port' => 6379,
        'database' => 0,
        'password' => '',
        'connectionTimeout' => 20,
    ]
],
  • 使用
// 前台发送
/* @var $redisPubSub RedisPubSub */
$redisPubSub = \Yii::$app->redisPubSub;
$redisPubSub->publish('test', 'xxxxxxxxxxxxxxx');

// console里面监听,并且处理,设置监听不超时
/* @var $redisPubSub RedisPubSub */
$redisPubSub = \Yii::$app->redisPubSub;
$redisPubSub->setOptReadTimeout(-1);
$redisPubSub->subscribe('test', function($instance, $channelName, $message) {
    var_dump($message);
});