在 PHP对Redis键空间通知过期事件的应用实例 一文中,已经介绍了PHP对Redis键空间通知功能的应用。本文主要介绍laravel中对Redis键空间通知过期事件的应用。
场景:用户下单后一个小时如果没付款就取消掉订单。
要实现这个,有了前文的基础,我们肯定不会只想到crontab定时去做,还会想到用Redis去实现。那么具体该怎么操作?请往下看:
1、先配置一下 .env
文件 ,缓存设置为Redis。
CACHE_DRIVER=redis
2、在控制器中新增订单的方法中加入缓存。
Cache::store('redis')->put('ORDER_CONFIRM:'.$order->id,$order->id,1); // 1分钟后过期。这里为了测试方便,暂设置为1分钟。
3、自定义artisan命令。
php artisan make:command OrderExpireListen
app\Console\Commands\OrderExpireListen.php文件
<?php
namespace App\Console\Commands;
use App\Model\Order;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
class OrderExpireListen extends Command
{
/**
* The name and signature of the conso