Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions config/geocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],

/*
|--------------------------------------------------------------------------
Expand Down
26 changes: 13 additions & 13 deletions src/ProviderAndDumperAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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));
}
Expand All @@ -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));
}
Expand All @@ -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));
}
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 8 additions & 7 deletions tests/Feature/Providers/GeocoderServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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}"));
}

/**
Expand Down Expand Up @@ -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}"));
}
Expand Down Expand Up @@ -303,7 +304,7 @@ public function testItHandlesOnlyCityAndState()

public function testEmptyResultsAreNotCached()
{
$cacheKey = str_slug(strtolower(urlencode('_')));
$cacheKey = md5(str_slug(strtolower(urlencode('_'))));

Geocoder::geocode('_')->get();

Expand Down
5 changes: 4 additions & 1 deletion tests/config/testConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
use Http\Client\Curl\Client;

return [
'cache-duration' => 999999999,
'cache' => [
'store' => null,
'duration' => 999999999,
],
'providers' => [
Chain::class => [
GeoIP2::class => [],
Expand Down