PHP7:Reids键空间通知配合TP5 实现分布式延时任务
测试环境:windows 10 phpStudy
配置redis配置文件 redis.windows.conf
notify-keyspace-events "Ex"
重启redis服务
重新打开一个控制台窗口,执行命令
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:
命令行完成了
二、借助TP5.1 的命令行工具
命令行工具的使用:https://www.kancloud.cn/manual/thinkphp5_1/354146
1、新建命令行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();
}
}
2、编写脚本 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)参数的值。当然,也有其它应用的情况。
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
6、最终的结果如下所示
更高级的慢慢扩展
1、自动取消订单
2、订单完成后发送短信
3、延迟任务等等
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tangcbkhf
系列文章
更多
同类精品
更多
-
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24 -
微信小程序没声音怎么办
PHP中文网 06-15 -
微信提示登录环境异常是什么意思原因
PHP中文网 04-09 -
excel图片置于文字下方的方法
PHP中文网 06-27 -
使用云服务器搭建个人游戏加速器教程
AuroraJay 07-06 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22 -
微信人名旁边有个图标有什么用
PHP中文网 03-11 -
Excel筛选和排序是灰色的怎么办
PHP中文网 06-22 -
抖音国际版要用什么加速器能流畅刷Tiktok的加速器
TK小达人 08-02 -
键盘上的负数是哪个键
PHP中文网 03-17