How to implement scheduled tasks in php redis

藏色散人
Release: 2023-03-17 08:32:02
Original
1405 people have browsed it

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 the scheduled task.

How to implement scheduled tasks in php redis

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How does php redis implement scheduled tasks?

php redis implements scheduled tasks

Modify the configuration file redis.conf

; notify-keyspace-events ""
Copy after login

to

notify-keyspace-events "Ex"
Copy after login

Notes:

1.Linux is normal Configuration

2. Configure under windows, `notify-keyspace-events ""` does not have the previous comment by default. You can choose to modify it directly here or comment out the current line, and look up for `; notify -keyspace-events "Ex"` Open the previous comment

3. Restart the redis service

php demo.php

<?php
$redis = new Redis();
$redis->connect(&#39;192.168.31.111&#39;, &#39;6379&#39;);
$redis->setOption(Redis::OPT_READ_TIMEOUT, -1);
$redis->setEx(&#39;k1&#39;, 3, 5); // 3 秒过期
//$redis_db = &#39;0&#39;; // 监听 0 号库
$redis_db = &#39;*&#39;; // 监听所有库
$redis->psubscribe([
    &#39;__keyevent@&#39; . $redis_db . &#39;__:expired&#39;
], &#39;keyCallback&#39;);
// 回调方法
function keyCallback($redis, $pattern, $channel, $msg)
{
    var_dump($redis);
    var_dump($pattern);
    var_dump($channel);
    var_dump($msg);
}
Copy after login

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"
Copy after login

redis-cli

setex foo 3 bar
Copy after login

Recommended study: "PHP Video Tutorial"

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template