Files
webman-buildadmin/app/common/service/GameHotDataWriteQueue.php
zhenhui 1eed3cf0f7 1.增加互斥锁:保证缓存和数据库数据一致性
2.增加消费队列,保证mysql数据的正常保存
2026-04-20 14:13:48 +08:00

67 lines
1.8 KiB
PHP
Raw Permalink 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
declare(strict_types=1);
namespace app\common\service;
use support\Redis;
use Throwable;
/**
* 热点缓存回源刷新队列LPUSH / RPOP由独立进程消费削峰 Redis 与 DB 回读。
* 业务线程在落库后仍会同步刷新缓存;队列任务为幂等补偿,避免瞬时高峰打满连接。
*/
final class GameHotDataWriteQueue
{
public const OP_USER_REFRESH = 'user.refresh';
public const OP_GC_REFRESH = 'gc.refresh';
public const OP_GR_SYNC = 'gr.sync';
public static function queueListKey(): string
{
$k = config('game_hot_cache.queue_list_key', 'dfw:q:hot_data_write');
return is_string($k) && $k !== '' ? $k : 'dfw:q:hot_data_write';
}
public static function enabled(): bool
{
return filter_var(config('game_hot_cache.enable_cache_write_queue', true), FILTER_VALIDATE_BOOLEAN);
}
/**
* @param array<string, mixed> $job 须含 op 字段
*/
public static function enqueue(array $job): void
{
if (!self::enabled()) {
return;
}
if (!isset($job['op']) || !is_string($job['op']) || $job['op'] === '') {
return;
}
$job['v'] = 1;
$job['ts'] = time();
$max = self::maxQueueLength();
try {
if ($max > 0) {
$len = Redis::lLen(self::queueListKey());
if (is_int($len) && $len >= $max) {
Redis::rPop(self::queueListKey());
}
}
Redis::lPush(self::queueListKey(), json_encode($job, JSON_UNESCAPED_UNICODE));
} catch (Throwable) {
}
}
public static function maxQueueLength(): int
{
$v = config('game_hot_cache.queue_max_length', 50000);
$n = filter_var($v, FILTER_VALIDATE_INT);
return ($n === false || $n < 0) ? 50000 : $n;
}
}