From 1ea4e291eab2193f2e5bf4f89bf11b5aa5faf2a1 Mon Sep 17 00:00:00 2001 From: Markus Schlotbohm Date: Sat, 17 Mar 2018 15:28:44 +0100 Subject: [PATCH 1/3] Added the ability to specify a cache store to use --- config/geocoder.php | 41 +++++++++++++++---- src/ProviderAndDumperAggregator.php | 18 ++++---- .../Feature/Providers/GeocoderServiceTest.php | 7 ++-- tests/config/testConfig.php | 5 ++- 4 files changed, 51 insertions(+), 20 deletions(-) diff --git a/config/geocoder.php b/config/geocoder.php index 96e3c5c..0784071 100644 --- a/config/geocoder.php +++ b/config/geocoder.php @@ -9,17 +9,44 @@ /* |-------------------------------------------------------------------------- - | Cache Duration + | Cache Settings |-------------------------------------------------------------------------- | - | Specify the cache duration in minutes. The default approximates a forever - | cache, but there are certain issues with Laravel's forever caching - | methods that prevent us from using them in this project. - | - | Default: 9999999 (integer) + | Here you may define all of the cache settings. | */ - 'cache-duration' => 9999999, + + 'cache' => [ + + /* + |-------------------------------------------------------------------------- + | Cache Store + |-------------------------------------------------------------------------- + | + | Specify the cache store to use for caching. The default value null will + | use the default cache store specified in the cache.php cofig file. + | + | Default: null + | + */ + + 'store' => null, + + /* + |-------------------------------------------------------------------------- + | Cache Duration + |-------------------------------------------------------------------------- + | + | Specify the cache duration in minutes. The default approximates a forever + | cache, but there are certain issues with Laravel's forever caching + | methods that prevent us from using them in this project. + | + | Default: 9999999 (integer) + | + */ + + 'duration' => 9999999, + ], /* |-------------------------------------------------------------------------- diff --git a/src/ProviderAndDumperAggregator.php b/src/ProviderAndDumperAggregator.php index 5b25b64..1634299 100644 --- a/src/ProviderAndDumperAggregator.php +++ b/src/ProviderAndDumperAggregator.php @@ -80,9 +80,9 @@ public function dump(string $dumper) : Collection public function geocodeQuery(GeocodeQuery $query) : self { $cacheKey = serialize($query); - $this->results = app('cache')->remember( + $this->results = app('cache')->store(config('geocoder.cache.store', null))->remember( "geocoder-{$cacheKey}", - config('geocoder.cache-duration', 0), + config('geocoder.cache.duration', 0), function () use ($query) { return collect($this->aggregator->geocodeQuery($query)); } @@ -96,9 +96,9 @@ function () use ($query) { public function reverseQuery(ReverseQuery $query) : self { $cacheKey = serialize($query); - $this->results = app('cache')->remember( + $this->results = app('cache')->store(config('geocoder.cache.store', null))->remember( "geocoder-{$cacheKey}", - config('geocoder.cache-duration', 0), + config('geocoder.cache.duration', 0), function () use ($query) { return collect($this->aggregator->reverseQuery($query)); } @@ -117,9 +117,9 @@ public function getName() : string public function geocode(string $value) : self { $cacheKey = str_slug(strtolower(urlencode($value))); - $this->results = app('cache')->remember( + $this->results = app('cache')->store(config('geocoder.cache.store', null))->remember( "geocoder-{$cacheKey}", - config('geocoder.cache-duration', 0), + config('geocoder.cache.duration', 0), function () use ($value) { return collect($this->aggregator->geocode($value)); } @@ -133,9 +133,9 @@ function () use ($value) { public function reverse(float $latitude, float $longitude) : self { $cacheKey = str_slug(strtolower(urlencode("{$latitude}-{$longitude}"))); - $this->results = app('cache')->remember( + $this->results = app('cache')->store(config('geocoder.cache.store', null))->remember( "geocoder-{$cacheKey}", - config('geocoder.cache-duration', 0), + config('geocoder.cache.duration', 0), function () use ($latitude, $longitude) { return collect($this->aggregator->reverse($latitude, $longitude)); } @@ -264,7 +264,7 @@ protected function getAdapterClass(string $provider) : string protected function removeEmptyCacheEntry(string $cacheKey) { - $result = app('cache')->get($cacheKey); + $result = app('cache')->store(config('geocoder.cache.store', null))->get($cacheKey); if ($result && $result->isEmpty()) { app('cache')->forget($cacheKey); diff --git a/tests/Feature/Providers/GeocoderServiceTest.php b/tests/Feature/Providers/GeocoderServiceTest.php index 5ab454f..698d1db 100644 --- a/tests/Feature/Providers/GeocoderServiceTest.php +++ b/tests/Feature/Providers/GeocoderServiceTest.php @@ -141,7 +141,8 @@ public function testItThrowsAnExceptionForInvalidDumper() public function testConfig() { - $this->assertEquals(999999999, config('geocoder.cache-duration')); + $this->assertEquals(null, config('geocoder.cache.store')); + $this->assertEquals(999999999, config('geocoder.cache.duration')); $this->assertTrue(is_array($providers = $this->app['config']->get('geocoder.providers'))); $this->assertCount(3, $providers); $this->assertArrayHasKey(GoogleMaps::class, $providers[Chain::class]); @@ -169,8 +170,8 @@ public function testCacheIsUsed() $result = app('geocoder')->geocode('1600 Pennsylvania Ave NW, Washington, DC 20500, USA') ->get(); - $this->assertTrue(app('cache')->has("geocoder-{$cacheKey}")); - $this->assertEquals($result, app('cache')->get("geocoder-{$cacheKey}")); + $this->assertTrue(app('cache')->store(config('geocoder.cache.store', null))->has("geocoder-{$cacheKey}")); + $this->assertEquals($result, app('cache')->store(config('geocoder.cache.store', null))->get("geocoder-{$cacheKey}")); } /** diff --git a/tests/config/testConfig.php b/tests/config/testConfig.php index 0f4d377..2c64bd6 100644 --- a/tests/config/testConfig.php +++ b/tests/config/testConfig.php @@ -16,7 +16,10 @@ use Http\Client\Curl\Client; return [ - 'cache-duration' => 999999999, + 'cache' => [ + 'store' => null, + 'duration' => 999999999, + ], 'providers' => [ Chain::class => [ GeoIP2::class => [], From 0c513fe701531a60690174cc87959d5d08ce2a25 Mon Sep 17 00:00:00 2001 From: Markus Schlotbohm Date: Sat, 17 Mar 2018 18:53:49 +0100 Subject: [PATCH 2/3] Added md5 encrytion to the cache key --- src/ProviderAndDumperAggregator.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ProviderAndDumperAggregator.php b/src/ProviderAndDumperAggregator.php index 1634299..c5d01f9 100644 --- a/src/ProviderAndDumperAggregator.php +++ b/src/ProviderAndDumperAggregator.php @@ -79,7 +79,7 @@ public function dump(string $dumper) : Collection public function geocodeQuery(GeocodeQuery $query) : self { - $cacheKey = serialize($query); + $cacheKey = md5(serialize($query)); $this->results = app('cache')->store(config('geocoder.cache.store', null))->remember( "geocoder-{$cacheKey}", config('geocoder.cache.duration', 0), @@ -95,7 +95,7 @@ function () use ($query) { public function reverseQuery(ReverseQuery $query) : self { - $cacheKey = serialize($query); + $cacheKey = md5(serialize($query)); $this->results = app('cache')->store(config('geocoder.cache.store', null))->remember( "geocoder-{$cacheKey}", config('geocoder.cache.duration', 0), @@ -116,7 +116,7 @@ public function getName() : string public function geocode(string $value) : self { - $cacheKey = str_slug(strtolower(urlencode($value))); + $cacheKey = md5(str_slug(strtolower(urlencode($value)))); $this->results = app('cache')->store(config('geocoder.cache.store', null))->remember( "geocoder-{$cacheKey}", config('geocoder.cache.duration', 0), @@ -132,7 +132,7 @@ function () use ($value) { public function reverse(float $latitude, float $longitude) : self { - $cacheKey = str_slug(strtolower(urlencode("{$latitude}-{$longitude}"))); + $cacheKey = md5(str_slug(strtolower(urlencode("{$latitude}-{$longitude}")))); $this->results = app('cache')->store(config('geocoder.cache.store', null))->remember( "geocoder-{$cacheKey}", config('geocoder.cache.duration', 0), From 3cc763a34029fe3f6bb793071cadafbe2b25cbe1 Mon Sep 17 00:00:00 2001 From: Markus Schlotbohm Date: Sun, 18 Mar 2018 14:36:44 +0100 Subject: [PATCH 3/3] Fixed geocoder service tests to use md5 cache keys --- tests/Feature/Providers/GeocoderServiceTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Feature/Providers/GeocoderServiceTest.php b/tests/Feature/Providers/GeocoderServiceTest.php index 698d1db..ad8318d 100644 --- a/tests/Feature/Providers/GeocoderServiceTest.php +++ b/tests/Feature/Providers/GeocoderServiceTest.php @@ -165,7 +165,7 @@ public function testGeocoder() public function testCacheIsUsed() { - $cacheKey = str_slug(strtolower(urlencode('1600 Pennsylvania Ave NW, Washington, DC 20500, USA'))); + $cacheKey = md5(str_slug(strtolower(urlencode('1600 Pennsylvania Ave NW, Washington, DC 20500, USA')))); $result = app('geocoder')->geocode('1600 Pennsylvania Ave NW, Washington, DC 20500, USA') ->get(); @@ -265,14 +265,14 @@ public function testGetProvider() public function testJapaneseCharacterGeocoding() { - $cacheKey = str_slug(strtolower(urlencode('108-0075 東京都港区港南2丁目16-3'))); + $cacheKey = md5(str_slug(strtolower(urlencode('108-0075 東京都港区港南2丁目16-3')))); app('geocoder')->geocode('108-0075 東京都港区港南2丁目16-3') ->get(); $this->assertEquals( $cacheKey, - '108-0075e69db1e4baace983bde6b8afe58cbae6b8afe58d97efbc92e4b881e79baeefbc91efbc96efbc8defbc93' + md5('108-0075e69db1e4baace983bde6b8afe58cbae6b8afe58d97efbc92e4b881e79baeefbc91efbc96efbc8defbc93') ); $this->assertTrue(app('cache')->has("geocoder-{$cacheKey}")); } @@ -304,7 +304,7 @@ public function testItHandlesOnlyCityAndState() public function testEmptyResultsAreNotCached() { - $cacheKey = str_slug(strtolower(urlencode('_'))); + $cacheKey = md5(str_slug(strtolower(urlencode('_')))); Geocoder::geocode('_')->get();