From 2ef6fc3c8806f96832f27cf2829f7a2e9e7d5098 Mon Sep 17 00:00:00 2001 From: xiaomlove <1939737565@qq.com> Date: Tue, 21 Jan 2025 13:24:10 +0800 Subject: [PATCH] add cache for isIPSeedBoxFromASN --- include/globalfunctions.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/include/globalfunctions.php b/include/globalfunctions.php index 956f6690..29401186 100644 --- a/include/globalfunctions.php +++ b/include/globalfunctions.php @@ -863,6 +863,9 @@ function do_action($name, ...$args) function isIPSeedBoxFromASN($ip, $exceptionWhenYes = false): bool { + $redis = \Nexus\Database\NexusDB::redis(); + $key = "nexus_asn"; + $notFoundCacheValue = "__NOT_FOUND__"; try { static $reader; $database = nexus_env('GEOIP2_ASN_DATABASE'); @@ -878,10 +881,23 @@ function isIPSeedBoxFromASN($ip, $exceptionWhenYes = false): bool if ($asn <= 0) { return false; } + $cacheResult = $redis->hGet($key, $asn); + if ($cacheResult !== false) { + if ($cacheResult === $notFoundCacheValue) { + return false; + } else { + return true; + } + } $row = \Nexus\Database\NexusDB::getOne("seed_box_records", "asn = $asn", "id"); + if (!empty($row)) { + $redis->hSet($key, $asn, $row['id']); + } else { + $redis->hSet($key, $asn, $notFoundCacheValue); + } } catch (\Throwable $throwable) { - do_log("ip: $ip, error: " . $throwable->getMessage(), "error"); - return false; + do_log("ip: $ip, " . $throwable->getMessage()); + $redis->hSet($key, $asn, $notFoundCacheValue); } $result = !empty($row); if ($result && $exceptionWhenYes) {