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..c5d01f9 100644 --- a/src/ProviderAndDumperAggregator.php +++ b/src/ProviderAndDumperAggregator.php @@ -79,10 +79,10 @@ public function dump(string $dumper) : Collection public function geocodeQuery(GeocodeQuery $query) : self { - $cacheKey = serialize($query); - $this->results = app('cache')->remember( + $cacheKey = md5(serialize($query)); + $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)); } @@ -95,10 +95,10 @@ function () use ($query) { public function reverseQuery(ReverseQuery $query) : self { - $cacheKey = serialize($query); - $this->results = app('cache')->remember( + $cacheKey = md5(serialize($query)); + $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)); } @@ -116,10 +116,10 @@ public function getName() : string public function geocode(string $value) : self { - $cacheKey = str_slug(strtolower(urlencode($value))); - $this->results = app('cache')->remember( + $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), + config('geocoder.cache.duration', 0), function () use ($value) { return collect($this->aggregator->geocode($value)); } @@ -132,10 +132,10 @@ function () use ($value) { public function reverse(float $latitude, float $longitude) : self { - $cacheKey = str_slug(strtolower(urlencode("{$latitude}-{$longitude}"))); - $this->results = app('cache')->remember( + $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), + 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..ad8318d 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]); @@ -164,13 +165,13 @@ 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(); - $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}")); } /** @@ -264,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}")); } @@ -303,7 +304,7 @@ public function testItHandlesOnlyCityAndState() public function testEmptyResultsAreNotCached() { - $cacheKey = str_slug(strtolower(urlencode('_'))); + $cacheKey = md5(str_slug(strtolower(urlencode('_')))); Geocoder::geocode('_')->get(); 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 => [],