欢迎光临
我们一直在努力

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

本篇文章主要给大家介绍reids 键空间通知配合tp5 实现分布式延时任务,希望对需要的朋友有所帮助!

1e572951d89f44eef79b80328c622bf.png

测试环境:windows 10 + phpStudy

配置redis配置文件 redis.windows.conf

notify-keyspace-events "Ex"

重启redis服务

f82303ef78b1835d19a4c809e5bed8f.png

重新打开一个控制台窗口,执行命令

psubscribe __keyevent@0__:expired

打开新窗口执行了阻塞订阅操作后的终端,等会会有信息输出:

C:Usersadmin>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

命令行完成了

二、借助TP5.1 的命令行工具

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

1、新建命令行pay

<?php
/**.-------------------------------------------------------------------------------------------------------------------
 * |  Github: https://github.com/Tinywan
 * '------------------------------------------------------------------------------------------------------------------*/
 
namespace appcommoncommand;
 
use apppayserviceRedisSubscribe;
use thinkconsoleCommand;
use thinkconsoleInput;
use thinkconsoleinputArgument;
use thinkconsoleOutput;
 
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 apppayservice;
 
use redisBaseRedis;
use thinkfacadeLog;
 
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:Usersadmin>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

7b0aefb0bbf05f1ab67c300e1182e74.png

6、最终的结果如下所示

aa6a20826cadb091c3cca0d72c04bc6.png

更高级的慢慢扩展

1、自动取消订单

2、订单完成后发送短信

3、延迟任务等等

相关推荐:《PHP7教程


以上就是【PHP7之Reids键空间通知配合TP5 实现分布式延时任务】的详细内容。

想要了解更多内容,请持续关注码农资源网,一起探索发现编程世界的无限可能!

本站部分资源来源于网络,仅限用于学习和研究目的,请勿用于其他用途。

如有侵权请发送邮件至1943759704@qq.com删除

赞(0) 打赏
未经允许不得转载:码农资源网 » PHP7之Reids键空间通知配合TP5 实现分布式延时任务
分享到

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册