Laravel Redis键过期,supervisor 进程管理实现15分钟取消订单

1、自动取消订单考虑到Redis键过期进行处理,修改Redis配置文件,并重启Redis
复制代码
notify-keyspace-events "Ex"
2、Laravel安装Redis扩展
复制代码
composer require predis/predis
3、Laravel配置Redis连接
复制代码
这里不详细介绍,.env  config\database.php 配置即可
4、建立任务文件
复制代码
php artisan make:command OrderCloseExpireListen

app\Console\Commands\OrderCloseExpireListen

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Log;
class OrderCloseExpireListen extends Command
{
/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = 'orderClose:expire';

/**
 * The console command description.
 *
 * @var string
 */
protected $description = '15分钟自动取消订单';

/**
 * Create a new command instance.
 *
 * @return void
 */
public function __construct()
{
    parent::__construct();
}

/**
 * @param $number
 * @return false|string
 */
public function str_after($number){
    $str = substr($number,strripos($number,":")+1);
    return $str;
}

/**
 * @param $number
 * @return false|string
 */
public function str_before($number){
    $str = substr($number,0,strrpos($number,":"));
    return $str;
}


/**
 * Execute the console command.
 *
 * @return int
 */
public function handle()
{
    $db = config('database.redis.default.database', 0); // 监听的数据库
    $pattern = '__keyevent@'. $db . '__:expired'; // 监听事件:键过期事件
    Redis::subscribe([$pattern], function ($channel) { // 订阅键过期事件,$channel为返回的键名
        $key_type = $this->str_before($channel);
		if($key_type == 'ORDER_CLOSE_ID'){
		    $order_id = $this->str_after($channel);    // 获取订单ID
            $order = (new Order())->getOne(['id'=>(int)$order_id]);
			//取消订单业务逻辑
		}           
    });
}

}

4、执行
复制代码
php artisan orderClose:expire
使用supervisor进程管理
centos 安装: yum install supervisor 
启动服务:supervisord -c /etc/supervisord.conf
修改配置:vim /etc/supervisord.conf
files = supervisord.d/\*.conf // 注意:编译器问题用的时候去掉\
cd /etc/supervisord.d/
vim orderClose.conf

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /项目目录/artisan orderClose:expire
autostart=true
autorestart=true
user=root
numprocs=8
redirect_stderr=true
stderr_logfile=/etc/supervisord.d/log/ossoffical.err.log #错误日志文件
stdout_logfile=/etc/supervisord.d/log/ossoffical.out.log #输出日志文件

supervisorctl reload  //重新加载配置文件
5、存Redis
复制代码
Redis::set('ORDER_CLOSE_ID:1', 1, 'EX', 900); // 设置ORDER_CLOSE_ID:1这个键15分钟后过期
相关推荐
ALex_zry19 小时前
Redis Cluster 分布式缓存架构设计与实践
redis·分布式·缓存
毕设源码-邱学长19 小时前
【开题答辩全过程】以 基于PHP的发热病人管理平台的设计与实现为例,包含答辩的问题和答案
开发语言·php
catchadmin21 小时前
2026 年 PHP 前后端分离后台管理系统推荐 企业级方案
开发语言·php
小李独爱秋21 小时前
计算机网络经典问题透视:蜂窝网络切换如何“扼杀”你的TCP连接?
网络·网络协议·tcp/ip·计算机网络·php·信息与通信
Data_Journal21 小时前
如何使用 Python 解析 JSON 数据
大数据·开发语言·前端·数据库·人工智能·php
serve the people21 小时前
python环境搭建 (十三) tenacity重试库
服务器·python·php
乔江seven1 天前
【Flask 进阶】3 从同步到异步:基于 Redis 任务队列解决 API 高并发与长耗时任务阻塞
redis·python·flask
darkb1rd1 天前
五、PHP类型转换与类型安全
android·安全·php
这周也會开心1 天前
Redis与MySQL回写中的数据类型存储设计
数据库·redis·mysql
shuair1 天前
redis缓存预热、缓存击穿、缓存穿透、缓存雪崩
redis·spring·缓存