refactor: scope postcode cache to place names, DB is authoritative for postcodes

This commit is contained in:
Ovidiu U
2026-04-22 12:23:50 +01:00
parent d460de1850
commit 5426722c71
2 changed files with 38 additions and 15 deletions

View File

@@ -26,7 +26,16 @@ class PostcodeService
public function resolve(string $query): ?LocationResult
{
$query = trim($query);
$cacheKey = 'postcode:'.strtolower(preg_replace('/\s+/', '', $query));
if ($this->isFullPostcode($query)) {
return $this->lookupLocalPostcode($query) ?? $this->lookupPostcode($query);
}
if ($this->isOutcode($query)) {
return $this->lookupLocalOutcode($query) ?? $this->lookupOutcode($query);
}
$cacheKey = 'place:'.strtolower(preg_replace('/\s+/', '', $query));
$cached = Cache::get($cacheKey);
@@ -34,11 +43,7 @@ class PostcodeService
return $cached;
}
$result = match (true) {
$this->isFullPostcode($query) => $this->lookupLocalPostcode($query) ?? $this->lookupPostcode($query),
$this->isOutcode($query) => $this->lookupLocalOutcode($query) ?? $this->lookupOutcode($query),
default => $this->lookupPlace($query),
};
$result = $this->lookupPlace($query);
if ($result !== null) {
Cache::put($cacheKey, $result, self::CACHE_TTL);