PHP7之Reids键空间通知配合TP5 实现分布式延时任务-PHP7

资源魔 48 0
本篇文章次要给各人引见Reids 键空间告诉合营TP5 完成散布式延时义务,心愿对需求的冤家有所协助!

1e572951d89f44eef79b80328c622bf.png

测试环境:windows 10 + phpStudy

设置装备摆设redis设置装备摆设文件 redis.windows.conf

notify-keyspace-events "Ex"

重启redis效劳

f82303ef78b1835d19a4c809e5bed8f.png

从新关上一个管制台窗口,执行饬令

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:
05c43475f1596a28dbecf6ea32ec16f.png

饬令行实现了

2、借助TP5.1 的饬令行对象

饬令行对象的应用:https://www.kancloud.cn/manual/thinkphp5_1/354146

一、新建饬令行pay

<?php
/**.-------------------------------------------------------------------------------------------------------------------
 * |  Github: https://github.com/Tinywan
 * '------------------------------------------------------------------------------------------------------------------*/
 
namespace app\co妹妹on\co妹妹and;
 
use app\pay\service\RedisSubscribe;
use think\console\Co妹妹and;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
 
class Pay extends Co妹妹and
{
    // 设置装备摆设指令
    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 co妹妹and 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,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

7b0aefb0bbf05f1ab67c300e1182e74.png

六、终极的后果以下所示

aa6a20826cadb091c3cca0d72c04bc6.png

更初级的缓缓扩大

一、主动勾销定单

二、定单实现后发送短信

三、提早义务等等

相干保举:《PHP7教程》

以上就是PHP7之Reids键空间告诉合营TP5 完成散布式延时义务的具体内容,更多请存眷资源魔其它相干文章!

标签: PHP7 TP5 php7开发教程 php7开发资料 php7开发自学 Reids 分布式延时任务

抱歉,评论功能暂时关闭!