Fixes #6130 -- Fix storage of listener IP geolocation data in cache.

This commit is contained in:
Buster Neece 2023-03-03 15:39:25 -06:00
parent a077cd3f3c
commit 2bdf611fec
No known key found for this signature in database
GPG Key ID: F1D2E64A0005E80E
1 changed files with 26 additions and 22 deletions

View File

@ -73,32 +73,11 @@ final class IpGeolocation
$this->initialize();
}
$reader = $this->reader;
if (null === $reader) {
throw new RuntimeException('No IP Geolocation reader available.');
}
$cacheKey = $this->readerShortName . '_' . str_replace([':', '.'], '_', $ip);
$cacheItem = $this->psr6Cache->getItem($cacheKey);
if (!$cacheItem->isHit()) {
try {
$ipInfo = $reader->get($ip);
if (!empty($ipInfo)) {
return $ipInfo;
}
$cacheItem->set([
'status' => 'error',
'message' => 'Internal/Reserved IP',
]);
} catch (Exception $e) {
$cacheItem->set([
'status' => 'error',
'message' => $e->getMessage(),
]);
}
$cacheItem->set($this->getUncachedLocationInfo($ip));
/** @noinspection SummerTimeUnsafeTimeManipulationInspection */
$cacheItem->expiresAfter(86400 * 7);
@ -110,4 +89,29 @@ final class IpGeolocation
return IpGeolocator\IpResult::fromIpInfo($ip, $ipInfo);
}
private function getUncachedLocationInfo(string $ip): array
{
$reader = $this->reader;
if (null === $reader) {
throw new RuntimeException('No IP Geolocation reader available.');
}
try {
$ipInfo = $reader->get($ip);
if (!empty($ipInfo)) {
return $ipInfo;
}
return [
'status' => 'error',
'message' => 'Internal/Reserved IP',
];
} catch (Exception $e) {
return [
'status' => 'error',
'message' => $e->getMessage(),
];
}
}
}