PHP7 學習筆記(十四)Reids 鍵空間通知配合TP5 實現分佈式延時任務

測試環境:windows 10 + phpStudyphp

配置redis配置文件 redis.windows.confgit

notify-keyspace-events "Ex"

重啓redis服務github

從新打開一個控制檯窗口,執行命令 redis

psubscribe __keyevent@0__:expired

 打開新窗口執行了阻塞訂閱操做後的終端,等會會有信息輸出:thinkphp

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:windows

命令行完成了bash

2、藉助TP5.1 的命令行工具閉包

 命令行工具的使用:https://www.kancloud.cn/manual/thinkphp5_1/354146app

一、新建命令行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();
    }
}

 二、編寫腳本 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, $callback ) 方法的第二個參數爲一個回調函數,這裏我使用閉包做爲一個回調

 官方解釋:匿名函數(Anonymous functions),也叫閉包函數(closures),容許 臨時建立一個沒有指定名稱的函數。最常常用做回調函數(callback)參數的值。固然,也有其它應用的狀況。

 三、在TP5 項目根目錄執行pay 命令工具

php think pay psubscribe

 四、新打開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>

五、查看打日誌文件,看有沒有接收到過時的key

六、最終的結果以下所示

 更高級的慢慢擴展

一、自動取消訂單

二、訂單完成後發送短信

三、延遲任務等等

相關文章
相關標籤/搜索