iankibet/redis-sub 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

iankibet/redis-sub

最新稳定版本:1.0.8

Composer 安装命令:

composer require iankibet/redis-sub

包简介

A Laravel package to listen to Redis published messages.

README 文档

README

Latest Stable Version Total Downloads License

A Laravel package for listening to Redis published messages and handling them with jobs, events, or other handlers.

Installation

You can install the package via Composer:

composer require iankibet/redis-sub

Publish Configuration

After installation, publish the package configuration file:

php artisan vendor:publish --tag=redis-sub

This will create a config/redis-sub.php file where you can define the Redis channels and their handlers.

Configuration

In config/redis-sub.php, define the Redis channels and their corresponding handlers:

return [
    'channels' => [
        'members' => [
            \App\Jobs\ProcessMemberMessage::class,
            \App\Listeners\MemberListener::class,
        ],
        'notifications' => [
            \App\Events\NotificationReceived::class,
        ],
    ],
];

Handlers

Handlers can be:

  • Jobs (e.g., ProcessMemberMessage that implements ShouldQueue).
  • Events (e.g., NotificationReceived that uses the Dispatchable trait).
  • Callable Classes (e.g., MemberListener with an __invoke method or handle method).

Example Handlers

Job Example

<?php

namespace App\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;

class ProcessMemberMessage implements ShouldQueue
{
    protected $message;

    public function __construct($message)
    {
        $this->message = $message;
    }

    public function handle()
    {
        logger()->info("Processed job message: {$this->message}");
    }
}

Event Example

<?php

namespace App\Events;

use Illuminate\Foundation\Events\Dispatchable;

class NotificationReceived
{
    use Dispatchable;

    public $message;

    public function __construct($message)
    {
        $this->message = $message;
    }
}

Listener Example

<?php

namespace App\Listeners;

class MemberListener
{
    public function __invoke($message)
    {
        logger()->info("Handled message with callable: {$message}");
    }
}

Usage

Run the Redis subscriber command to listen to the configured channels:

php artisan redis:subscribe

The command will listen for messages published on the Redis channels and dispatch the configured handlers.

Example Logs

When a message is published to the members channel, you'll see output like this:

[2024-11-24 15:30:15] Dispatched job: App\Jobs\ProcessMemberMessage for channel: members
[2024-11-24 15:30:15] Called handler: App\Listeners\MemberListener for channel: members

Debugging

  • Timestamps are included in log messages for better debugging.
  • Ensure the Redis service is running and accessible by Laravel.

Contributing

Feel free to submit issues or pull requests. Contributions are welcome!

License

The MIT License (MIT). Please see LICENSE for more information.


---

### Key Updates:
1. **Added Packagist Badges**: To display version, downloads, and license status.
2. **Installation Command**: Direct install command using `composer require iankibet/redis-sub`.
3. **Configuration Section**: Clear explanation of how to use the published configuration file.
4. **Usage and Debugging**: Detailed examples of expected behavior and logs.
5. **License**: Reference to the `LICENSE` file.

Let me know if you'd like additional refinements!

统计信息

  • 总下载量: 478
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-24