8000 Added functionality to not cache requests by using `doNotCache()`. · geocoder-php/GeocoderLaravel@47665e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 47665e4

Browse files
committed
Added functionality to not cache requests by using doNotCache().
1 parent e34555b commit 47665e4

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [4.3.3] - 2020-06-20
6+
### Added
7+
- functionality to not cache requests by using `doNotCache()`.
8+
59
## [4.3.0] - 2020-02-29
610
### Added
711
- Laravel 7 compatibility.

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ Finally, configure Geocoder for Laraver to use this store. Edit
9595
],
9696
```
9797

98+
#### Disabling Caching on a Query-Basis
99+
You can disable caching on a query-by-query basis as needed, like so:
100+
```php
101+
$results = app("geocoder")
102+
->doNotCache()
103+
->geocode('Los Angeles, CA')
104+
->get();
105+
```
106+
98107
### Providers
99108
If you are upgrading and have previously published the geocoder config file, you
100109
need to add the `cache-duration` variable, otherwise cache will be disabled

src/ProviderAndDumperAggregator.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class ProviderAndDumperAggregator
3232
protected $aggregator;
3333
protected $limit;
3434
protected $results;
35+
protected $isCaching = true;
3536

3637
public function __construct()
3738
{
@@ -59,6 +60,13 @@ public function toJson() : string
5960
->first();
6061
}
6162

63+
public function doNotCache() : self
64+
{
65+
$this->isCaching = false;
66+
67+
return $this;
68+
}
69+
6270
public function dump(string $dumper) : Collection
6371
{
6472
$dumperClasses = collect([
@@ -181,6 +189,10 @@ public function using(string $name) : self
181189

182190
protected function cacheRequest(string $cacheKey, array $queryElements, string $queryType)
183191
{
192+
if (! $this->isCaching) {
193+
return collect($this->aggregator->{$queryType}(...$queryElements));
194+
}
195+
184196
$hashedCacheKey = sha1($cacheKey);
185197
$duration = config("geocoder.cache.duration", 0);
186198
$store = config('geocoder.cache.store');

tests/Feature/Providers/GeocoderServiceTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,17 @@ public function testEmptyResultsAreNotCached()
313313

314314
$this->assertFalse(app('cache')->has("geocoder-{$cacheKey}"));
315315
}
316+
317+
public function testCachingCanBeDisabled()
318+
{
319+
$results = app("geocoder")
320+
->doNotCache()
321+
->geocode('Los Angeles, CA')
322+
->get();
323+
324+
$this->assertEquals(
325+
"Los Angeles, CA, USA",
326+
$results->first()->getFormattedAddress()
327+
);
328+
}
316329
}

0 commit comments

Comments
 (0)
0