From 20b262968975a559d6f95f8cf42d61def478c7f6 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Fri, 27 Oct 2017 14:27:28 -0700 Subject: [PATCH] Fixed geocoder to not cache if results are empty --- CHANGELOG.md | 5 +++++ src/ProviderAndDumperAggregator.php | 17 +++++++++++++++++ .../Providers/GeocoderServiceTest.php | 9 +++++++++ 3 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a8d183..5a05b28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## [4.0.3] - 27 Oct 2017 +### Fixed +- cache duration to work on 32-bit systems. +- geocoder to not cache if no results are provided. + ## [4.0.2] - 2 Sep 2017 ### Fixed - erroneous method `getProvider()` and marked it as deprecated. diff --git a/src/ProviderAndDumperAggregator.php b/src/ProviderAndDumperAggregator.php index 0fbcf5f..c8558d2 100644 --- a/src/ProviderAndDumperAggregator.php +++ b/src/ProviderAndDumperAggregator.php @@ -88,6 +88,8 @@ function () use ($query) { } ); + $this->removeEmptyCacheEntry("geocoder-{$cacheKey}"); + return $this; } @@ -102,6 +104,8 @@ function () use ($query) { } ); + $this->removeEmptyCacheEntry("geocoder-{$cacheKey}"); + return $this; } @@ -121,6 +125,8 @@ function () use ($value) { } ); + $this->removeEmptyCacheEntry("geocoder-{$cacheKey}"); + return $this; } @@ -135,6 +141,8 @@ function () use ($latitude, $longitude) { } ); + $this->removeEmptyCacheEntry("geocoder-{$cacheKey}"); + return $this; } @@ -239,4 +247,13 @@ protected function getAdapterClass(string $provider) : string return config('geocoder.adapter'); } + + protected function removeEmptyCacheEntry(string $cacheKey) + { + $result = app('cache')->get($cacheKey); + + if ($result && $result->isEmpty()) { + app('cache')->forget($cacheKey); + } + } } diff --git a/tests/Laravel5_5/Providers/GeocoderServiceTest.php b/tests/Laravel5_5/Providers/GeocoderServiceTest.php index 09be506..1d2e0a2 100644 --- a/tests/Laravel5_5/Providers/GeocoderServiceTest.php +++ b/tests/Laravel5_5/Providers/GeocoderServiceTest.php @@ -308,4 +308,13 @@ public function testItHandlesOnlyCityAndState() $this->assertEquals('Washington', $results->first()->getAdminLevels()->first()->getName()); $this->assertEquals('United States', $results->first()->getCountry()->getName()); } + + public function testEmptyResultsAreNotCached() + { + $cacheKey = str_slug(strtolower(urlencode('_'))); + + Geocoder::geocode('_')->get(); + + $this->assertFalse(app('cache')->has("geocoder-{$cacheKey}")); + } }