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

点击蓝字bdc8db3bdce1e5cab058ae368fd0bd7e.png关注我们!每天获取最新的编程小知识!

源 / php中文网      源 / www.php.cn

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

070e0cd1fdc8ed3aa8576cb682306f6e.png

(查看原文请点击本文末尾左下角:

测试环境:windows 10 + phpStudy

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

notify-keyspace-events "Ex"

重启redis服务

fe5d4fcbad5a7efd6ac5ee86f4c99430.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:

b00fbecdba23d2734baee5553a24edb8.png

命令行完成了

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

命令行工具的使用:

https://www.kancloud.cn/manual/thinkphp5_1/354146

1、新建命令行pay

/**.-------------------------------------------------------------------------------------------------------------------

 * |  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

/**.-------------------------------------------------------------------------------------------------------------------

 * |  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

b734e05874b4e1c7379d5db01a8695ed.png

6、最终的结果如下所示

5c2001bdabcd5c3bf35d2d9c108570d0.png

更高级的慢慢扩展

1、自动取消订单

2、订单完成后发送短信

3、延迟任务等等

16abd6986a7f8e1d9077cc26c6d4d4e5.gif

-END-

声明:本文选自「 php中文网 」,搜索「 phpcnnew 」即可关注!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值