php redis method to implement scheduled tasks: 1. Modify the content of the configuration file redis.conf to "notify-keyspace-events "Ex""; 2. Restart the redis service; 3. Pass "object(Redis)#1(0){}string(22) "__keyevent@*__:expired"string(22) "__keyevent@0__:expire..." Just implement scheduled tasks.
php redis implements scheduled tasks
Modify the configuration file redis.conf
; notify-keyspace-events ""
to
notify-keyspace-events "Ex"
Note:
1.Linux normal configuration
2. Configure under windows, `notify-keyspace-events ""` does not have previous comments by default. You can choose to modify it directly here or comment out the current line and go up Find the comment in front of `; notify-keyspace-events "Ex"` and open it
3. Restart the redis service
php demo.php
<?php $redis = new Redis(); $redis->connect('192.168.31.111', '6379'); $redis->setOption(Redis::OPT_READ_TIMEOUT, -1); $redis->setEx('k1', 3, 5); // 3 秒过期 //$redis_db = '0'; // 监听 0 号库 $redis_db = '*'; // 监听所有库 $redis->psubscribe([ '__keyevent@' . $redis_db . '__:expired' ], 'keyCallback'); // 回调方法 function keyCallback($redis, $pattern, $channel, $msg) { var_dump($redis); var_dump($pattern); var_dump($channel); var_dump($msg); }
Start the test
php demo.php
Result after 3 seconds
object(Redis)#1 (0) { } string(22) "__keyevent@*__:expired" string(22) "__keyevent@0__:expired" string(2) "k1"
redis-cli
setex foo 3 bar
The above is the detailed content of How to implement scheduled tasks in php redis. For more information, please follow other related articles on the PHP Chinese website!