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分钟后过期
相关推荐
曲幽2 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
BingoGo1 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack1 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
BingoGo2 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack2 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack3 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo3 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
BingoGo4 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·laravel
JaguarJack4 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php