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

六、最终的结果以下所示

 更高级的慢慢扩展

一、自动取消订单

二、订单完成后发送短信

三、延迟任务等等

相关文章
相关标签/搜索