mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-24 03:57:22 +08:00
add cache for isIPSeedBoxFromASN
This commit is contained in:
@@ -863,6 +863,9 @@ function do_action($name, ...$args)
|
|||||||
|
|
||||||
function isIPSeedBoxFromASN($ip, $exceptionWhenYes = false): bool
|
function isIPSeedBoxFromASN($ip, $exceptionWhenYes = false): bool
|
||||||
{
|
{
|
||||||
|
$redis = \Nexus\Database\NexusDB::redis();
|
||||||
|
$key = "nexus_asn";
|
||||||
|
$notFoundCacheValue = "__NOT_FOUND__";
|
||||||
try {
|
try {
|
||||||
static $reader;
|
static $reader;
|
||||||
$database = nexus_env('GEOIP2_ASN_DATABASE');
|
$database = nexus_env('GEOIP2_ASN_DATABASE');
|
||||||
@@ -878,10 +881,23 @@ function isIPSeedBoxFromASN($ip, $exceptionWhenYes = false): bool
|
|||||||
if ($asn <= 0) {
|
if ($asn <= 0) {
|
||||||
return false;
|
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");
|
$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) {
|
} catch (\Throwable $throwable) {
|
||||||
do_log("ip: $ip, error: " . $throwable->getMessage(), "error");
|
do_log("ip: $ip, " . $throwable->getMessage());
|
||||||
return false;
|
$redis->hSet($key, $asn, $notFoundCacheValue);
|
||||||
}
|
}
|
||||||
$result = !empty($row);
|
$result = !empty($row);
|
||||||
if ($result && $exceptionWhenYes) {
|
if ($result && $exceptionWhenYes) {
|
||||||
|
|||||||
Reference in New Issue
Block a user