The application of PHP to Redis keyspace notification has been introduced in the application example of PHP to Redis keyspace notification. This paper mainly introduces the application of Redis key space notification expiration event in Laravel.

Scenario: The user cancels the order if he/she fails to pay within an hour of placing the order.

To implement this, given the foundation of the previous article, we will definitely not only think about crontab timing, but also think about Redis implementation. So how do you do it? Read on:

Env. Set cache to Redis.

CACHE_DRIVER=redis

2. Add a cache to the controller’s new order method.

Cache::store('redis')->put('ORDER_CONFIRM:'.$order->id,$order->id,1); // It will expire after 1 minute. For the convenience of testing, set this parameter to 1 minute.Copy the code

3. Customize the artisan command.

php artisan make:command OrderExpireListen

App, Console, Commands, OrderExpireListen. PHP file


      
 
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 console command.
     *
     * @var string
     */
    protected $signature = 'order:expire';
 
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Listen for order creation and cancel the order after 1 minute if no payment is made. ';
 
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
 
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $cachedb = config('database.redis.cache.database'.0);
        $pattern = '__keyevent@' . $cachedb . '__:expired';
        Redis::subscribe($pattern.function ($channel) {     // Subscribe key expiration event
            // laravel_cache:ORDER_CONFIRM:7
            $channel = trim(strstr($channel.':'), ':');
            $key_type = str_before($channel.':');
            switch ($key_type) {
                case 'ORDER_CONFIRM':
                    $order_id = str_after($channel.':');    // Retrieve the order ID
                    $order = Order::query()->find($order_id);
                    if ($order) {
                        // Perform the cancel operation
                        $order->status = 2;
                        $order->save();
                    }
                    break;
                case 'ORDER_OTHEREVENT':
                    break;
                default:
                    break; }}); }}Copy the code

Laravel_cache :ORDER_CONFIRM:7 This can be queried using the following method:

127.0.0.1:6379> keys *
1) "laravel_cache:ORDER_CONFIRM:7"
Copy the code

After expiration, we can see the following in the Redis client:

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 1) "pmessage" 2) "__keyevent@0__:expired" 3) "__keyevent@0__:expired" 4) "laravel_cache:5c785c036466adea360111aa28563bfd556b5fba" 1) "pmessage" 2) "__keyevent@0__:expired" 3) "__keyevent@0__:expired" 4) "laravel_cache:5c785c036466adea360111aa28563bfd556b5fba:timer" 1) "pmessage" 2) "__keyevent@0__:expired" 3) "__keyevent@0__:expired" 4) "laravel_cache:ORDER_CONFIRM:7"Copy the code