Skip to content

Commit 20b2629

Browse files
committed
Fixed geocoder to not cache if results are empty
1 parent 488cd65 commit 20b2629

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [4.0.3] - 27 Oct 2017
6+
### Fixed
7+
- cache duration to work on 32-bit systems.
8+
- geocoder to not cache if no results are provided.
9+
510
## [4.0.2] - 2 Sep 2017
611
### Fixed
712
- erroneous method `getProvider()` and marked it as deprecated.

src/ProviderAndDumperAggregator.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ function () use ($query) {
8888
}
8989
);
9090

91+
$this->removeEmptyCacheEntry("geocoder-{$cacheKey}");
92+
9193
return $this;
9294
}
9395

@@ -102,6 +104,8 @@ function () use ($query) {
102104
}
103105
);
104106

107+
$this->removeEmptyCacheEntry("geocoder-{$cacheKey}");
108+
105109
return $this;
106110
}
107111

@@ -121,6 +125,8 @@ function () use ($value) {
121125
}
122126
);
123127

128+
$this->removeEmptyCacheEntry("geocoder-{$cacheKey}");
129+
124130
return $this;
125131
}
126132

@@ -135,6 +141,8 @@ function () use ($latitude, $longitude) {
135141
}
136142
);
137143

144+
$this->removeEmptyCacheEntry("geocoder-{$cacheKey}");
145+
138146
return $this;
139147
}
140148

@@ -239,4 +247,13 @@ protected function getAdapterClass(string $provider) : string
239247

240248
return config('geocoder.adapter');
241249
}
250+
251+
protected function removeEmptyCacheEntry(string $cacheKey)
252+
{
253+
$result = app('cache')->get($cacheKey);
254+
255+
if ($result && $result->isEmpty()) {
256+
app('cache')->forget($cacheKey);
257+
}
258+
}
242259
}

tests/Laravel5_5/Providers/GeocoderServiceTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,13 @@ public function testItHandlesOnlyCityAndState()
308308
$this->assertEquals('Washington', $results->first()->getAdminLevels()->first()->getName());
309309
$this->assertEquals('United States', $results->first()->getCountry()->getName());
310310
}
311+
312+
public function testEmptyResultsAreNotCached()
313+
{
314+
$cacheKey = str_slug(strtolower(urlencode('_')));
315+
316+
Geocoder::geocode('_')->get();
317+
318+
$this->assertFalse(app('cache')->has("geocoder-{$cacheKey}"));
319+
}
311320
}

0 commit comments

Comments
 (0)