Skip to content

Commit

Permalink
Added functionality to not cache requests by using doNotCache().
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebronner committed Jun 20, 2020
1 parent e34555b commit 47665e4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [4.3.3] - 2020-06-20
### Added
- functionality to not cache requests by using `doNotCache()`.

## [4.3.0] - 2020-02-29
### Added
- Laravel 7 compatibility.
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ Finally, configure Geocoder for Laraver to use this store. Edit
],
```

#### Disabling Caching on a Query-Basis
You can disable caching on a query-by-query basis as needed, like so:
```php
$results = app("geocoder")
->doNotCache()
->geocode('Los Angeles, CA')
->get();
```

### Providers
If you are upgrading and have previously published the geocoder config file, you
need to add the `cache-duration` variable, otherwise cache will be disabled
Expand Down
12 changes: 12 additions & 0 deletions src/ProviderAndDumperAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ProviderAndDumperAggregator
protected $aggregator;
protected $limit;
protected $results;
protected $isCaching = true;

public function __construct()
{
Expand Down Expand Up @@ -59,6 +60,13 @@ public function toJson() : string
->first();
}

public function doNotCache() : self
{
$this->isCaching = false;

return $this;
}

public function dump(string $dumper) : Collection
{
$dumperClasses = collect([
Expand Down Expand Up @@ -181,6 +189,10 @@ public function using(string $name) : self

protected function cacheRequest(string $cacheKey, array $queryElements, string $queryType)
{
if (! $this->isCaching) {
return collect($this->aggregator->{$queryType}(...$queryElements));
}

$hashedCacheKey = sha1($cacheKey);
$duration = config("geocoder.cache.duration", 0);
$store = config('geocoder.cache.store');
Expand Down
13 changes: 13 additions & 0 deletions tests/Feature/Providers/GeocoderServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,17 @@ public function testEmptyResultsAreNotCached()

$this->assertFalse(app('cache')->has("geocoder-{$cacheKey}"));
}

public function testCachingCanBeDisabled()
{
$results = app("geocoder")
->doNotCache()
->geocode('Los Angeles, CA')
->get();

$this->assertEquals(
"Los Angeles, CA, USA",
$results->first()->getFormattedAddress()
);
}
}

0 comments on commit 47665e4

Please sign in to comment.