PHP 使用redis 实现定时任务

直接上代码吧

配置文件中开启键空间通知  notify-keyspace-events Ex

1.新建index.php

$redis = new \Redis();
//连接本地的 Redis 服务
$redis->connect("127.0.0.1", 6379);

$redis->setOption(\Redis::OPT_READ_TIMEOUT,-1);
echo "订阅消息监听 \n";
$redis->psubscribe(array('__keyevent@0__:expired'), 'psCallback');

function psCallback($redis, $pattern, $chan, $msg)
{
    echo "Pattern: $pattern\n";
    echo "Channel: $chan\n";
    echo "Msg: $msg\n";
}

执行php index.php

2.新建文件index2.php

$redis = new \Redis();
//连接本地的 Redis 服务
$redis->connect("127.0.0.1", 6379);
$redis->set("name", "test");
$redis->expire("name", 3);

3.3秒后输出

posted @ 2020-04-24 15:20  禾火意  阅读(2061)  评论(0编辑  收藏  举报