ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
​ 强烈推荐使用think-queue ,[传送门 ](https://blog.csdn.net/qq_34050360/article/details/116986237 "传送门 ")  一、安装Redis 二、修改Redis配置如下所示,并重启: 这里需要配置 notify-keyspace-events 的参数为 “Ex”。x 代表了过期事件。notify-keyspace-events “Ex” 保存配置后,重启Redis服务,使配置生效。 三、安装php的Redis扩展 四、在  \\think\\cache\\driver\\Redis()  添加几个方法,或者重新定义一个Redis方法: ~~~php /*自定义Redis*/ public function setex($key, $time, $val) { return $this->handler->setex($key, $time, $val); } public function expire($key = null, $time = 0) { return $this->handler->expire($key, $time); } public function psubscribe($patterns = array(), $callback) { $this->handler->psubscribe($patterns, $callback); } public function setOption() { $this->handler->setOption(\Redis::OPT_READ_TIMEOUT, -1); } /*自定义Redis*/ ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") 五、application\\command.php  新增一个   ~~~php <?php return [ 'app\api\controller\Gopush', //脚本推送进程常驻 ]; ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") 六、新建控制器 app\\api\\controller\\Gopush ~~~php <?php namespace app\api\controller; use think\Controller; use think\Request; use think\console\Command; use think\console\Input; use think\console\Output; class Gopush extends Command { protected function configure() { $this->setName('gopush')->setDescription('Here is the gopush'); } protected function execute(Input $input, Output $output) { $redis = new \think\cache\driver\Redis(); $redis->setOption(); // $redis->psubscribe(array('__keyevent@0__:expired'), 'keyCallback'); $redis->psubscribe(array('__keyevent@0__:expired'), function ($redis, $pattern, $channel, $msg){ echo PHP_EOL; echo "Pattern: $pattern\n"; echo "Channel: $channel\n"; echo "Payload: $msg\n\n"; //................ /*TODO处理业务逻辑*/ }); $output->writeln("TestCommand:998998998"); } // public static function keyCallback($redis, $pattern, $chan, $msg) // { // echo "Pattern: $pattern\n"; // echo "Channel: $chan\n"; // echo "Payload: $msg\n\n"; // //keyCallback为订阅事件后的回调函数,这里写业务处理逻辑, // //比如前面提到的商品不支付自动撤单,这里就可以根据订单id,来实现自动撤单 // } } ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") ### 七、执行命令  php think gopush 让进程在终端运行(后面放到常驻进程里面去 supervisor),如下图所示 (**重点:每次修改了东西,守护进程要进行重启才有效**)(启动如果一直失败,就将启动用户改为root) ![](https://img-blog.csdnimg.cn/20201020162512685.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MDUwMzYw,size_16,color_FFFFFF,t_70)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")​编辑 ![](https://img-blog.csdnimg.cn/2020102015380388.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MDUwMzYw,size_16,color_FFFFFF,t_70)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")​编辑 八、触发方法:$key 可以用来做需要的内容数据拼接,时间可以根据订单倒计时算出来秒数,第三个参数只是备注 ~~~php /*这个格式去推送消息-用户设置的时候处理*/ public function r3(){ $re=new Redis(); $key='推送别名-内容ID-用户ID'; $re->setex($key,3,'redis延迟任务'); //3秒后回调 } ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") 九、thinkphp自定义命令 [自定义命令行 · ThinkPHP5.0完全开发手册 · 看云](https://www.kancloud.cn/manual/thinkphp5/235129 "自定义命令行 · ThinkPHP5.0完全开发手册 · 看云")  可以看着理解,很简单的 十、参考博客,都是原生的,最后还得封装,我都给你们封装好了 [参考地址一](https://blog.csdn.net/weixin_30535043/article/details/99357330?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param "参考地址一") [参考地址二](https://www.cnblogs.com/idjl/p/9610538.html "参考地址二") [参考地址三](https://blog.csdn.net/jj546630576/article/details/80340573?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param "参考地址三") [参考地址四](https://blog.csdn.net/assasin0308/article/details/90261104?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param "参考地址四") [参考地址五](https://github.com/chenlinzhong/php-delayqueue "参考地址五") [参考地址六](https://blog.csdn.net/weixin_30856965/article/details/96166152?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param "参考地址六") [参考地址七](https://blog.csdn.net/ligupeng7929/article/details/85206877?utm_medium=distribute.pc_relevant.none-task-blog-title-2&spm=1001.2101.3001.4242 "参考地址七") 十一、划重点 ~~~php /*$redis->psubscribe(array('__keyevent@'.$redis_db.'__:expired'), function ($redis, $pattern, $channel, $msg){ echo PHP_EOL; echo "Pattern: $pattern\n"; echo "Channel: $channel\n"; echo "Payload: $msg\n\n"; //................ });*/ ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") 点击链接加入群聊【ThinkPHP56小功能】:[正在跳转](https://jq.qq.com/?_wv=1027&k=Z0c641WP "正在跳转")  ​