搜尋
首頁php框架Swooleswoole如何監聽redis數據

swoole如何監聽redis數據

Apr 10, 2020 am 10:00 AM
swoole

swoole如何監聽redis數據

swoole#如何監聽redis資料?

Laravel使用swoole監聽redis

#開始之前,請先確保redis已經正確安裝,並且正常運作。

Laravel程式碼

在App\Events目錄下新建RedisTest事件

<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class RedisTest
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $message;
    /**
    * Create a new event instance.
    *
    * @return void
    */
    public function __construct($message)
    {
        $this->message = $message;
    }
    /**
    * Get the channels the event should broadcast on.
    *
    * @return \Illuminate\Broadcasting\Channel|array
    */
    public function broadcastOn()
    {
        return new PrivateChannel(&#39;channel-name&#39;);
    }
}

App\Listeners\RedisTestListener 監聽事件程式碼

<?php
namespace App\Listeners;
use App\Events\RedisTest;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
class RedisTestListener
{
    /**
    * Create the event listener.
    *
    * @return void
    */
    public function __construct()
    {
        //
    }
    /**
    * Handle the event.
    *
    * @param  RedisTest  $event
    * @return void
    */
    public function handle(RedisTest $event)
    {
        $message = $event->message;
        Log::info(&#39;the message received from subscribed redis channel msg_0: &#39;.$message);
    }
}

App\Providers\EventServiceProvider登記事件/監聽關係

protected $listen = [
        &#39;App\Events\RedisTest&#39; => [
            &#39;App\Listeners\RedisTestListener&#39;,
        ],
    ];

監聽指令

App\Console\Commands\RedisSubscribe 程式碼如下

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use swoole_redis;
use Illuminate\Support\Facades\Event;
use App\Events\RedisTest;
class RedisSubscribe extends Command
{
    /**
    * The name and signature of the console command.
    *
    * @var string
    */
    protected $signature = &#39;redis:subscribe&#39;;
    /**
    * The console command description.
    *
    * @var string
    */
    protected $description = &#39;deamon process to subscribe redis broadcast&#39;;
    /**
    * Create a new command instance.
    *
    * @return void
    */
    public function __construct()
    {
        parent::__construct();
    }
    /**
    * Execute the console command.
    *
    * @return mixed
    */
    public function handle()
    {
        $client = new swoole_redis;
        $client->on(&#39;message&#39;, function (swoole_redis $client, $result) {
            var_dump($result);
            static $more = false;
            if (!$more and $result[0] == &#39;message&#39;)
            {
                echo "trigger Event RedisTest\n";
                Event::fire(new RedisTest($result[2]));
            }
        });
        $client->connect(&#39;127.0.0.1&#39;, 6379, function (swoole_redis $client, $result) {
            echo "connect\n";
            $client->subscribe(&#39;msg_0&#39;);
        });
    }
}

Laravel部分程式碼完成

==== ==============================

supervisor 管理進程

在/etc/supervisor /conf.d 資料夾下新建echo.conf , 程式碼如下

[group:echos]
programs=echo-queue,echo-redis
[program:echo-queue]
command=php artisan queue:work
directory=/home/bella/Downloads/lnmp/echo1.0/echo
user=bella
autorestart=true
redirect_stderr=true
stdout_logfile=/home/bella/Downloads/lnmp/echo1.0/echo/storage/logs/queue.log
loglevel=info
[program:echo-redis]
command=php artisan redis:subscribe
directory=/home/bella/Downloads/lnmp/echo1.0/echo
user=bella
autorestart=true
redirect_stderr=true
stdout_logfile=/home/bella/Downloads/lnmp/echo1.0/echo/storage/logs/redis.log
loglevel=info

完成後,執行下列指令重載

supervisorctl reload

============== ===================

進入redis 用戶端,發布一個廣播通知到msg_0 頻道

publish msg_0 "Hello Bella"

如果laravel目錄下的storage \logs\laravel.log 最後的日誌中記錄了廣播發送的通知,則redis監聽功能實作

以上是swoole如何監聽redis數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
我該如何為Swoole開源項目做出貢獻?我該如何為Swoole開源項目做出貢獻?Mar 18, 2025 pm 03:58 PM

本文概述了為Swoole項目做出貢獻的方法,包括報告錯誤,提交功能,編碼和改進文檔。它討論了初學者開始貢獻的必要技能和步驟,以及如何找到緊迫的是

如何使用自定義模塊擴展Swoole?如何使用自定義模塊擴展Swoole?Mar 18, 2025 pm 03:57 PM

文章討論了使用自定義模塊,詳細的步驟,最佳實踐和故障排除擴展swoole。主要重點是增強功能和集成。

如何使用Swoole的異步I/O功能?如何使用Swoole的異步I/O功能?Mar 18, 2025 pm 03:56 PM

本文討論了在PHP中使用Swoole的異步I/O功能用於高性能應用程序。它涵蓋安裝,服務器設置和優化策略。單詞計數:159

如何配置Swoole的過程隔離?如何配置Swoole的過程隔離?Mar 18, 2025 pm 03:55 PM

文章討論了配置Swoole的流程隔離,其好處如提高穩定性和安全性以及故障排除方法。

Swoole的反應堆模型如何在引擎蓋下工作?Swoole的反應堆模型如何在引擎蓋下工作?Mar 18, 2025 pm 03:54 PM

Swoole的反應堆模型使用事件驅動的,非阻滯I/O架構來有效地管理高持續性場景,通過各種技術優化性能。(159個字符)(159個字符)

如何在Swoole中解決連接問題?如何在Swoole中解決連接問題?Mar 18, 2025 pm 03:53 PM

文章討論了對PHP框架Swoole中的連接問題的故障排除,原因,監視和預防。

我可以使用什麼工具來監視Swoole的性能?我可以使用什麼工具來監視Swoole的性能?Mar 18, 2025 pm 03:52 PM

本文討論了監視和優化Swoole的性能的工具和最佳實踐,以及針對性能問題的故障排除方法。

如何解決Swoole應用程序中的內存洩漏?如何解決Swoole應用程序中的內存洩漏?Mar 18, 2025 pm 03:51 PM

摘要:本文討論了通過識別,隔離和固定解決SWOORE應用程序中的內存洩漏,並強調了常見原因,例如不當資源管理和不受管理的Coroutines。 Swoole Tracker和Valgrind等工具

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具