p_ysk/app/process/ExpiredRedis.php

48 lines
1.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\process;
use support\Log;
use Webman\RedisQueue\Redis;
use Workerman\Redis\Client;
// 消息推送
class ExpiredRedis
{
public function onWorkerStart()
{
$resis_host = config('redis.default.host');
$resis_port = config('redis.default.port');
$resis_pwd= config('redis.default.password');
$redis = new Client('redis://' .$resis_host. ':' . $resis_port);
// 如果Redis需要密码认证
$redis->auth($resis_pwd);
// 选择数据库默认0
$redis->select(0);
// 设置读取超时(-1表示不超时
$redis->setOption(\Redis::OPT_READ_TIMEOUT, -1);
// 订阅键过期事件频道
// __keyevent@0__:expired 表示监听0号数据库的键过期事件
$redis->psubscribe(['__keyevent@0__:expired'], function ($pattern, $channel, $key) use($redis){
// 例如根据键名前缀处理不同业务
// 营销短信
$str = 'expired:sms:';
if (strpos($key, $str) === 0) {
$id = str_replace($str, '', $key);
// 发给队列
Redis::send('send.mark.sms', '1,' . $id);
Log::info ("处理营销短信过期: {$id}\n{}");
}
// 微信模版
$str = 'expired:wechat:';
if (strpos($key, $str) === 0) {
$id = str_replace($str, '', $key);
// 发给队列
Redis::send('send.wechat.temp', '1,' . $id);
Log::info ("处理微信模版消息过期: {$id}\n{}");
}
});
}
}