本篇文章主要介紹Reids 鍵空間通知配合TP5 實作分散式縮時任務,希望對需要的朋友有幫助!
測試環境:windows 10 phpStudy
設定redis設定檔 redis.windows.conf
notify-keyspace-events "Ex"
重啟redis服務
重新開啟一個控制台窗口,執行指令
psubscribe __keyevent@0__:expired
開啟新視窗執行了阻塞訂閱作業後的終端,等一下會有訊息輸出:
C:\Users\admin>redis-cli 127.0.0.1:6379> psubscribe __keyevent@0__:expired Reading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "__keyevent@0__:expired" 3) (integer) 1
再開啟一個終端,redis-cli 進入redis,新增一個6秒過期的鍵username:
命令列完成了
二、借助TP5 .1 的命令列工具
命令列工具的使用:https://www.kancloud.cn/manual/thinkphp5_1/354146
1、新建命令列pay
<?php /**.------------------------------------------------------------------------------------------------------------------- * | Github: https://github.com/Tinywan * '------------------------------------------------------------------------------------------------------------------*/ namespace app\common\command; use app\pay\service\RedisSubscribe; use think\console\Command; use think\console\Input; use think\console\input\Argument; use think\console\Output; class Pay extends Command { // 配置指令 public function configure() { $this->setName('pay') ->addArgument('type', Argument::REQUIRED, "the type of the task that pay needs to run") ->setDescription('this is payment system command line tools'); } // 执行指令 public function execute(Input $input, Output $output) { $type = $input->getArgument('type'); if ($type == 'psubscribe') { // 发布订阅任务 $this->psubscribe(); } } /** * Redis 发布订阅模式 */ private function psubscribe() { $service = new RedisSubscribe(); $service->sub(); } }
2、寫腳本 RedisSubscribe.php
<?php /**.------------------------------------------------------------------------------------------------------------------- * | Github: https://github.com/Tinywan * '------------------------------------------------------------------------------------------------------------------*/ namespace app\pay\service; use redis\BaseRedis; use think\facade\Log; class RedisSubscribe { public function sub() { Log::error(get_current_date().'--过期事件的订阅-- '); $redis = BaseRedis::location(); //这里是直接连接本地redis $redis->setOption(\Redis::OPT_READ_TIMEOUT, -1); $redis->psubscribe(array('__keyevent@0__:expired'), function ($redis, $pattern, $chan, $msg) { Log::error('[1]--过期事件的订阅 ' . $msg); }); } }
說明:psubscribe( patterns,patterns,callback ) 方法的第二個參數為一個回呼函數,這裡我使用閉包作為一個回調。
官方解釋:匿名函數(Anonymous functions),也叫閉包函數(closures),允許 暫時建立一個沒有指定名稱的函數。最常用作回呼函數(callback)參數的值。當然,也有其它所應用的情況。
3、在TP5 專案根目錄執行pay 指令工具
php think pay psubscribe
4、新開啟console 視窗終端
C:\Users\admin>redis-cli 127.0.0.1:6379> setex UserName 10 Tinywan OK 127.0.0.1:6379> get UserName "Tinywan" 127.0.0.1:6379> get UserName (nil) 127.0.0.1:6379>
5、查看打日誌文件,看有沒有接收到過期的key
6、最終的結果如下圖
更進階的慢慢擴展
#1、自動取消訂單
2、訂單完成後發送簡訊
3、延遲任務等等
相關推薦:《PHP7教學》
以上是PHP7之Reids鍵空間通知配合TP5 實作分散式延時任務的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!