Fixes #6130 -- Fix storage of listener IP geolocation data in cache.
This commit is contained in:
parent
a077cd3f3c
commit
2bdf611fec
|
@ -73,32 +73,11 @@ final class IpGeolocation
|
||||||
$this->initialize();
|
$this->initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
$reader = $this->reader;
|
|
||||||
if (null === $reader) {
|
|
||||||
throw new RuntimeException('No IP Geolocation reader available.');
|
|
||||||
}
|
|
||||||
|
|
||||||
$cacheKey = $this->readerShortName . '_' . str_replace([':', '.'], '_', $ip);
|
$cacheKey = $this->readerShortName . '_' . str_replace([':', '.'], '_', $ip);
|
||||||
|
|
||||||
$cacheItem = $this->psr6Cache->getItem($cacheKey);
|
$cacheItem = $this->psr6Cache->getItem($cacheKey);
|
||||||
|
|
||||||
if (!$cacheItem->isHit()) {
|
if (!$cacheItem->isHit()) {
|
||||||
try {
|
$cacheItem->set($this->getUncachedLocationInfo($ip));
|
||||||
$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(),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @noinspection SummerTimeUnsafeTimeManipulationInspection */
|
/** @noinspection SummerTimeUnsafeTimeManipulationInspection */
|
||||||
$cacheItem->expiresAfter(86400 * 7);
|
$cacheItem->expiresAfter(86400 * 7);
|
||||||
|
@ -110,4 +89,29 @@ final class IpGeolocation
|
||||||
|
|
||||||
return IpGeolocator\IpResult::fromIpInfo($ip, $ipInfo);
|
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(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue