Skip to content

Commit 1ea4e29

Browse files
committed
Added the ability to specify a cache store to use
1 parent 8487330 commit 1ea4e29

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

config/geocoder.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,44 @@
99

1010
/*
1111
|--------------------------------------------------------------------------
12-
| Cache Duration
12+
| Cache Settings
1313
|--------------------------------------------------------------------------
1414
|
15-
| Specify the cache duration in minutes. The default approximates a forever
16-
| cache, but there are certain issues with Laravel's forever caching
17-
| methods that prevent us from using them in this project.
18-
|
19-
| Default: 9999999 (integer)
15+
| Here you may define all of the cache settings.
2016
|
2117
*/
22-
'cache-duration' => 9999999,
18+
19+
'cache' => [
20+
21+
/*
22+
|--------------------------------------------------------------------------
23+
| Cache Store
24+
|--------------------------------------------------------------------------
25+
|
26+
| Specify the cache store to use for caching. The default value null will
27+
| use the default cache store specified in the cache.php cofig file.
28+
|
29+
| Default: null
30+
|
31+
*/
32+
33+
'store' => null,
34+
35+
/*
36+
|--------------------------------------------------------------------------
37+
| Cache Duration
38+
|--------------------------------------------------------------------------
39+
|
40+
| Specify the cache duration in minutes. The default approximates a forever
41+
| cache, but there are certain issues with Laravel's forever caching
42+
| methods that prevent us from using them in this project.
43+
|
44+
| Default: 9999999 (integer)
45+
|
46+
*/
47+
48+
'duration' => 9999999,
49+
],
2350

2451
/*
2552
|--------------------------------------------------------------------------

src/ProviderAndDumperAggregator.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ public function dump(string $dumper) : Collection
8080
public function geocodeQuery(GeocodeQuery $query) : self
8181
{
8282
$cacheKey = serialize($query);
83-
$this->results = app('cache')->remember(
83+
$this->results = app('cache')->store(config('geocoder.cache.store', null))->remember(
8484
"geocoder-{$cacheKey}",
85-
config('geocoder.cache-duration', 0),
85+
config('geocoder.cache.duration', 0),
8686
function () use ($query) {
8787
return collect($this->aggregator->geocodeQuery($query));
8888
}
@@ -96,9 +96,9 @@ function () use ($query) {
9696
public function reverseQuery(ReverseQuery $query) : self
9797
{
9898
$cacheKey = serialize($query);
99-
$this->results = app('cache')->remember(
99+
$this->results = app('cache')->store(config('geocoder.cache.store', null))->remember(
100100
"geocoder-{$cacheKey}",
101-
config('geocoder.cache-duration', 0),
101+
config('geocoder.cache.duration', 0),
102102
function () use ($query) {
103103
return collect($this->aggregator->reverseQuery($query));
104104
}
@@ -117,9 +117,9 @@ public function getName() : string
117117
public function geocode(string $value) : self
118118
{
119119
$cacheKey = str_slug(strtolower(urlencode($value)));
120-
$this->results = app('cache')->remember(
120+
$this->results = app('cache')->store(config('geocoder.cache.store', null))->remember(
121121
"geocoder-{$cacheKey}",
122-
config('geocoder.cache-duration', 0),
122+
config('geocoder.cache.duration', 0),
123123
function () use ($value) {
124124
return collect($this->aggregator->geocode($value));
125125
}
@@ -133,9 +133,9 @@ function () use ($value) {
133133
public function reverse(float $latitude, float $longitude) : self
134134
{
135135
$cacheKey = str_slug(strtolower(urlencode("{$latitude}-{$longitude}")));
136-
$this->results = app('cache')->remember(
136+
$this->results = app('cache')->store(config('geocoder.cache.store', null))->remember(
137137
"geocoder-{$cacheKey}",
138-
config('geocoder.cache-duration', 0),
138+
config('geocoder.cache.duration', 0),
139139
function () use ($latitude, $longitude) {
140140
return collect($this->aggregator->reverse($latitude, $longitude));
141141
}
@@ -264,7 +264,7 @@ protected function getAdapterClass(string $provider) : string
264264

265265
protected function removeEmptyCacheEntry(string $cacheKey)
266266
{
267-
$result = app('cache')->get($cacheKey);
267+
$result = app('cache')->store(config('geocoder.cache.store', null))->get($cacheKey);
268268

269269
if ($result && $result->isEmpty()) {
270270
app('cache')->forget($cacheKey);

tests/Feature/Providers/GeocoderServiceTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ public function testItThrowsAnExceptionForInvalidDumper()
141141

142142
public function testConfig()
143143
{
144-
$this->assertEquals(999999999, config('geocoder.cache-duration'));
144+
$this->assertEquals(null, config('geocoder.cache.store'));
145+
$this->assertEquals(999999999, config('geocoder.cache.duration'));
145146
$this->assertTrue(is_array($providers = $this->app['config']->get('geocoder.providers')));
146147
$this->assertCount(3, $providers);
147148
$this->assertArrayHasKey(GoogleMaps::class, $providers[Chain::class]);
@@ -169,8 +170,8 @@ public function testCacheIsUsed()
169170
$result = app('geocoder')->geocode('1600 Pennsylvania Ave NW, Washington, DC 20500, USA')
170171
->get();
171172

172-
$this->assertTrue(app('cache')->has("geocoder-{$cacheKey}"));
173-
$this->assertEquals($result, app('cache')->get("geocoder-{$cacheKey}"));
173+
$this->assertTrue(app('cache')->store(config('geocoder.cache.store', null))->has("geocoder-{$cacheKey}"));
174+
$this->assertEquals($result, app('cache')->store(config('geocoder.cache.store', null))->get("geocoder-{$cacheKey}"));
174175
}
175176

176177
/**

tests/config/testConfig.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
use Http\Client\Curl\Client;
1717

1818
return [
19-
'cache-duration' => 999999999,
19+
'cache' => [
20+
'store' => null,
21+
'duration' => 999999999,
22+
],
2023
'providers' => [
2124
Chain::class => [
2225
GeoIP2::class => [],

0 commit comments

Comments
 (0)