Skip to content

Commit 2ef6fc3

Browse files
committed
add cache for isIPSeedBoxFromASN
1 parent b3702cb commit 2ef6fc3

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

include/globalfunctions.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,9 @@ function do_action($name, ...$args)
863863

864864
function isIPSeedBoxFromASN($ip, $exceptionWhenYes = false): bool
865865
{
866+
$redis = \Nexus\Database\NexusDB::redis();
867+
$key = "nexus_asn";
868+
$notFoundCacheValue = "__NOT_FOUND__";
866869
try {
867870
static $reader;
868871
$database = nexus_env('GEOIP2_ASN_DATABASE');
@@ -878,10 +881,23 @@ function isIPSeedBoxFromASN($ip, $exceptionWhenYes = false): bool
878881
if ($asn <= 0) {
879882
return false;
880883
}
884+
$cacheResult = $redis->hGet($key, $asn);
885+
if ($cacheResult !== false) {
886+
if ($cacheResult === $notFoundCacheValue) {
887+
return false;
888+
} else {
889+
return true;
890+
}
891+
}
881892
$row = \Nexus\Database\NexusDB::getOne("seed_box_records", "asn = $asn", "id");
893+
if (!empty($row)) {
894+
$redis->hSet($key, $asn, $row['id']);
895+
} else {
896+
$redis->hSet($key, $asn, $notFoundCacheValue);
897+
}
882898
} catch (\Throwable $throwable) {
883-
do_log("ip: $ip, error: " . $throwable->getMessage(), "error");
884-
return false;
899+
do_log("ip: $ip, " . $throwable->getMessage());
900+
$redis->hSet($key, $asn, $notFoundCacheValue);
885901
}
886902
$result = !empty($row);
887903
if ($result && $exceptionWhenYes) {

0 commit comments

Comments
 (0)