Prechádzať zdrojové kódy

fix: cache zip code service

Gustavo Mantovani 1 týždeň pred
rodič
commit
0bca01929d
1 zmenil súbory, kde vykonal 23 pridanie a 4 odobranie
  1. 23 4
      app/Services/ZipCodeCoordinatesService.php

+ 23 - 4
app/Services/ZipCodeCoordinatesService.php

@@ -204,12 +204,25 @@ class ZipCodeCoordinatesService
 
     private function restoreFromCache(string $zipCode): bool
     {
-        $cached = Cache::get("zip-code-coordinates:{$zipCode}");
+        $cacheKey = "zip-code-coordinates:{$zipCode}";
+
+        $cached = Cache::get($cacheKey);
 
         if (! is_array($cached) || ! array_key_exists('found', $cached)) {
             return false;
         }
 
+        if (! $cached['found']) {
+            Cache::forget($cacheKey);
+
+            Log::info('ZipCodeCoordinates negative cache ignored', [
+                'zip_code' => $zipCode,
+                'reason'   => 'retry_lookup_after_previous_failure',
+            ]);
+
+            return false;
+        }
+
         $this->resolvedZipCodes[$zipCode] = $cached['found']
             ? $cached['coordinates']
             : null;
@@ -221,6 +234,14 @@ class ZipCodeCoordinatesService
     {
         $this->resolvedZipCodes[$zipCode] = $coordinates;
 
+        if ($coordinates === null) {
+            Log::info('ZipCodeCoordinates failure not cached', [
+                'zip_code' => $zipCode,
+            ]);
+
+            return;
+        }
+
         Cache::put(
             "zip-code-coordinates:{$zipCode}",
             [
@@ -228,9 +249,7 @@ class ZipCodeCoordinatesService
                 'coordinates' => $coordinates,
             ],
             now()->addSeconds(
-                $coordinates !== null
-                    ? (int) config('services.brasil_api.cep_cache_ttl', 2592000)
-                    : (int) config('services.brasil_api.cep_failure_cache_ttl', 600)
+                (int) config('services.brasil_api.cep_cache_ttl', 2592000)
             )
         );
     }