Skip to content

Commit

Permalink
Fixed geocoder to not cache if results are empty
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebronner committed Oct 27, 2017
1 parent 488cd65 commit 20b2629
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions src/ProviderAndDumperAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ function () use ($query) {
}
);

$this->removeEmptyCacheEntry("geocoder-{$cacheKey}");

return $this;
}

Expand All @@ -102,6 +104,8 @@ function () use ($query) {
}
);

$this->removeEmptyCacheEntry("geocoder-{$cacheKey}");

return $this;
}

Expand All @@ -121,6 +125,8 @@ function () use ($value) {
}
);

$this->removeEmptyCacheEntry("geocoder-{$cacheKey}");

return $this;
}

Expand All @@ -135,6 +141,8 @@ function () use ($latitude, $longitude) {
}
);

$this->removeEmptyCacheEntry("geocoder-{$cacheKey}");

return $this;
}

Expand Down Expand Up @@ -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);
}
}
}
9 changes: 9 additions & 0 deletions tests/Laravel5_5/Providers/GeocoderServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}"));
}
}

0 comments on commit 20b2629

Please sign in to comment.