Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Redis to implement a delayed task in PHP

Shulou Source: shulou.com Published: 2022-06-02 08:41:58 10月27日 Update

How to use Redis to implement a delayed task in PHP? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

(a) Business scenarios:

1. When a business is triggered, you need to start a scheduled task and perform another task within a specified time (such as automatic cancellation of orders, automatic completion of orders, etc.)

2. The keyspace notifications of redis sends an event after the key expires, and the client listening for this event can receive a notification.

(B) Service preparation:

1. Modify the reids configuration file (redis.conf) [window system configuration file is: redis.windows.conf]

Redis does not enable keyspace notifications by default, because it consumes cpu when enabled.

Note: E:keyevent event, which is published with the prefix _ _ keyevent@__

X: expiration event, which occurs when a key expires and is deleted

The original configuration was:

Notify-keyspace-events ""

Change the configuration as follows:

Notify-keyspace-events "Ex"

After saving the configuration, restart the Redis service to make the configuration effective

[root@chokingwin etc] # service redis-server restart / usr/local/redis/etc/redis.conf Stopping redis-server: [OK] Starting redis-server: [OK]

Restart redis on the window system, first change to the redis file directory, then turn off the redis service (redis-server-- service-stop), and then turn on (redis-server-- service-start)

(C) document code:

Phpredis implements subscription Keyspace notification, which can cancel the order automatically and complete the order automatically. The following is a test example

Create 4 files, and then modify the database and redis configuration parameters

Db.class.php

Tags: Data database command file run background subscription event method process problem script configuration task program monitor datasheet time time message Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi Linux macOS Shulou Tech Info