Skip to content

Commit 92383da

Browse files
committed
Invalidating cache
1 parent f7dd090 commit 92383da

17 files changed

Lines changed: 118 additions & 54 deletions
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use League\Flysystem\Filesystem;
66
use League\Flysystem\UnableToReadFile;
77
use Sammyjo20\SaloonCachePlugin\Data\CachedResponse;
8-
use Sammyjo20\SaloonCachePlugin\Interfaces\CacheDriver;
8+
use Sammyjo20\SaloonCachePlugin\Interfaces\CacheDriverInterface;
99

10-
class FlysystemDriver implements CacheDriver
10+
class FlysystemDriverInterface implements CacheDriverInterface
1111
{
1212
/**
1313
* @param Filesystem $store

src/Drivers/LaravelCacheDriver.php renamed to src/Drivers/LaravelCacheDriverInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use Illuminate\Contracts\Cache\Repository;
66
use Sammyjo20\SaloonCachePlugin\Data\CachedResponse;
7-
use Sammyjo20\SaloonCachePlugin\Interfaces\CacheDriver;
7+
use Sammyjo20\SaloonCachePlugin\Interfaces\CacheDriverInterface;
88

9-
class LaravelCacheDriver implements CacheDriver
9+
class LaravelCacheDriverInterface implements CacheDriverInterface
1010
{
1111
/**
1212
* @param Repository $store

src/Drivers/SimpleCacheDriver.php renamed to src/Drivers/SimpleCacheDriverInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
use Psr\SimpleCache\CacheInterface;
66
use Sammyjo20\SaloonCachePlugin\Data\CachedResponse;
7-
use Sammyjo20\SaloonCachePlugin\Interfaces\CacheDriver;
7+
use Sammyjo20\SaloonCachePlugin\Interfaces\CacheDriverInterface;
88

99
/**
1010
* PSR-16 Cache Driver
1111
*/
12-
class SimpleCacheDriver implements CacheDriver
12+
class SimpleCacheDriverInterface implements CacheDriverInterface
1313
{
1414
/**
1515
* @param CacheInterface $store

src/Http/Middleware/ExplicitCacheMiddleware.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use GuzzleHttp\Promise\FulfilledPromise;
99
use Sammyjo20\Saloon\Http\SaloonRequest;
1010
use Sammyjo20\SaloonCachePlugin\Data\CachedResponse;
11-
use Sammyjo20\SaloonCachePlugin\Interfaces\CacheDriver;
11+
use Sammyjo20\SaloonCachePlugin\Interfaces\CacheDriverInterface;
1212

1313
class ExplicitCacheMiddleware
1414
{
@@ -22,9 +22,9 @@ class ExplicitCacheMiddleware
2222
/**
2323
* The cache driver used for caching.
2424
*
25-
* @var CacheDriver
25+
* @var CacheDriverInterface
2626
*/
27-
protected CacheDriver $cacheDriver;
27+
protected CacheDriverInterface $cacheDriver;
2828

2929
/**
3030
* The TTL in seconds.
@@ -33,14 +33,23 @@ class ExplicitCacheMiddleware
3333
*/
3434
protected int $cacheTTL;
3535

36+
/**
37+
* Should the existing cache be invalidated?
38+
*
39+
* @var bool
40+
*/
41+
protected bool $invalidateCache = false;
42+
3643
/**
3744
* @param SaloonRequest $request
45+
* @param bool $invalidateCache
3846
*/
39-
public function __construct(SaloonRequest $request)
47+
public function __construct(SaloonRequest $request, bool $invalidateCache = false)
4048
{
4149
$this->request = $request;
4250
$this->cacheDriver = $request->cacheDriver();
4351
$this->cacheTTL = $request->cacheTTLInSeconds();
52+
$this->invalidateCache = $invalidateCache;
4453
}
4554

4655
/**
@@ -62,7 +71,7 @@ public function __invoke(callable $handler): callable
6271
// If the file is valid, then we should return the promise here.
6372

6473
if (isset($cacheFile)) {
65-
if ($cacheFile->isValid()) {
74+
if ($this->invalidateCache === false && $cacheFile->isValid()) {
6675
return new FulfilledPromise($cacheFile->getResponse()->withHeader('X-Saloon-Cache', 'Cached'));
6776
}
6877

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Sammyjo20\SaloonCachePlugin\Data\CachedResponse;
66

7-
interface CacheDriver
7+
interface CacheDriverInterface
88
{
99
/**
1010
* Store the cached response on the driver.

src/Traits/AlwaysCacheResponses.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Sammyjo20\Saloon\Constants\Saloon;
66
use Sammyjo20\Saloon\Http\SaloonRequest;
77
use Sammyjo20\Saloon\Http\SaloonResponse;
8-
use Sammyjo20\SaloonCachePlugin\Interfaces\CacheDriver;
8+
use Sammyjo20\SaloonCachePlugin\Interfaces\CacheDriverInterface;
99
use Sammyjo20\SaloonCachePlugin\Http\Middleware\ExplicitCacheMiddleware;
1010

1111
trait AlwaysCacheResponses
@@ -27,6 +27,13 @@ trait AlwaysCacheResponses
2727
*/
2828
protected bool $cachingEnabled = true;
2929

30+
/**
31+
* Should the existing cache be invalidated?
32+
*
33+
* @var bool
34+
*/
35+
protected bool $invalidateCache = false;
36+
3037
/**
3138
* Boot the Saloon plugin
3239
*
@@ -50,7 +57,7 @@ public function bootAlwaysCacheResponses(SaloonRequest $request): void
5057

5158
// Run the custom cache middleware.
5259

53-
$request->addHandler('saloonCache', new ExplicitCacheMiddleware($request));
60+
$request->addHandler('saloonCache', new ExplicitCacheMiddleware($request, $this->invalidateCache));
5461

5562
// We should also intercept the response and set the "cached" property to true.
5663

@@ -75,7 +82,7 @@ public function bootAlwaysCacheResponses(SaloonRequest $request): void
7582
* @throws \JsonException
7683
* @throws \Sammyjo20\Saloon\Exceptions\SaloonInvalidConnectorException
7784
*/
78-
protected function cacheKey(SaloonRequest $request, array $headers, bool $hashKey = true): string
85+
protected function cacheKey(SaloonRequest $request, array $headers): string
7986
{
8087
$requestUrl = $request->getFullRequestUrl();
8188
$className = get_class($request);
@@ -121,12 +128,24 @@ public function disableCaching(): self
121128
return $this;
122129
}
123130

131+
/**
132+
* Invalidate the current cache and refresh the cache.
133+
*
134+
* @return $this
135+
*/
136+
public function invalidateCache(): self
137+
{
138+
$this->invalidateCache = true;
139+
140+
return $this;
141+
}
142+
124143
/**
125144
* Return an instance of the cache driver that should be used.
126145
*
127146
* @return mixed
128147
*/
129-
abstract public function cacheDriver(): CacheDriver;
148+
abstract public function cacheDriver(): CacheDriverInterface;
130149

131150
/**
132151
* Define the cache TTL (Time-to-live) in seconds.

tests/Feature/CacheTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,41 @@
201201
expect($responseC->isCached())->toBeFalse();
202202
expect($responseC->json())->toEqual(['name' => 'Michael']);
203203
});
204+
205+
test('cache can be invalidated', function () {
206+
$mockClient = new MockClient([
207+
MockResponse::make(['name' => 'Sam']),
208+
MockResponse::make(['name' => 'Gareth']),
209+
MockResponse::make(['name' => 'Teo']),
210+
MockResponse::make(['name' => 'Mantas']),
211+
]);
212+
213+
$requestA = new CachedUserRequest();
214+
$responseA = $requestA->send($mockClient);
215+
216+
expect($responseA->isCached())->toBeFalse();
217+
expect($responseA->json())->toEqual(['name' => 'Sam']);
218+
219+
$requestB = new CachedUserRequest();
220+
$responseB = $requestB->send($mockClient);
221+
222+
// The response should now be cached...
223+
224+
expect($responseB->isCached())->toBeTrue();
225+
expect($responseB->json())->toEqual(['name' => 'Sam']);
226+
227+
$requestC = new CachedUserRequest();
228+
$requestC->invalidateCache();
229+
$responseC = $requestC->send($mockClient);
230+
231+
expect($responseC->isCached())->toBeFalse();
232+
expect($responseC->json())->toEqual(['name' => 'Teo']);
233+
234+
// Now just make sure that the new response is cached...
235+
236+
$requestD = new CachedUserRequest();
237+
$responseD = $requestD->send($mockClient);
238+
239+
expect($responseD->isCached())->toBeTrue();
240+
expect($responseD->json())->toEqual(['name' => 'Teo']);
241+
});

tests/Fixtures/Connectors/CachedConnector.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use League\Flysystem\Filesystem;
66
use Sammyjo20\Saloon\Http\SaloonConnector;
77
use League\Flysystem\Local\LocalFilesystemAdapter;
8-
use Sammyjo20\SaloonCachePlugin\Interfaces\CacheDriver;
9-
use Sammyjo20\SaloonCachePlugin\Drivers\FlysystemDriver;
8+
use Sammyjo20\SaloonCachePlugin\Interfaces\CacheDriverInterface;
9+
use Sammyjo20\SaloonCachePlugin\Drivers\FlysystemDriverInterface;
1010
use Sammyjo20\SaloonCachePlugin\Traits\AlwaysCacheResponses;
1111

1212
class CachedConnector extends SaloonConnector
@@ -18,9 +18,9 @@ public function defineBaseUrl(): string
1818
return testApi();
1919
}
2020

21-
public function cacheDriver(): CacheDriver
21+
public function cacheDriver(): CacheDriverInterface
2222
{
23-
return new FlysystemDriver(new Filesystem(new LocalFilesystemAdapter(cachePath())));
23+
return new FlysystemDriverInterface(new Filesystem(new LocalFilesystemAdapter(cachePath())));
2424
}
2525

2626
public function cacheTTLInSeconds(): int

tests/Fixtures/Requests/AdvancedCustomKeyCachedUserRequest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
use Sammyjo20\Saloon\Constants\Saloon;
77
use Sammyjo20\Saloon\Http\SaloonRequest;
88
use League\Flysystem\Local\LocalFilesystemAdapter;
9-
use Sammyjo20\SaloonCachePlugin\Interfaces\CacheDriver;
10-
use Sammyjo20\SaloonCachePlugin\Drivers\FlysystemDriver;
9+
use Sammyjo20\SaloonCachePlugin\Interfaces\CacheDriverInterface;
10+
use Sammyjo20\SaloonCachePlugin\Drivers\FlysystemDriverInterface;
1111
use Sammyjo20\SaloonCachePlugin\Traits\AlwaysCacheResponses;
1212
use Sammyjo20\SaloonCachePlugin\Tests\Fixtures\Connectors\TestConnector;
1313

@@ -24,9 +24,9 @@ public function defineEndpoint(): string
2424
return '/user';
2525
}
2626

27-
public function cacheDriver(): CacheDriver
27+
public function cacheDriver(): CacheDriverInterface
2828
{
29-
return new FlysystemDriver(new Filesystem(new LocalFilesystemAdapter(cachePath())));
29+
return new FlysystemDriverInterface(new Filesystem(new LocalFilesystemAdapter(cachePath())));
3030
}
3131

3232
public function cacheTTLInSeconds(): int

tests/Fixtures/Requests/CachedPostRequest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use Sammyjo20\Saloon\Http\SaloonRequest;
88
use Sammyjo20\Saloon\Traits\Plugins\HasJsonBody;
99
use League\Flysystem\Local\LocalFilesystemAdapter;
10-
use Sammyjo20\SaloonCachePlugin\Interfaces\CacheDriver;
11-
use Sammyjo20\SaloonCachePlugin\Drivers\FlysystemDriver;
10+
use Sammyjo20\SaloonCachePlugin\Interfaces\CacheDriverInterface;
11+
use Sammyjo20\SaloonCachePlugin\Drivers\FlysystemDriverInterface;
1212
use Sammyjo20\SaloonCachePlugin\Traits\AlwaysCacheResponses;
1313
use Sammyjo20\SaloonCachePlugin\Tests\Fixtures\Connectors\TestConnector;
1414

@@ -33,9 +33,9 @@ public function defaultData(): array
3333
];
3434
}
3535

36-
public function cacheDriver(): CacheDriver
36+
public function cacheDriver(): CacheDriverInterface
3737
{
38-
return new FlysystemDriver(new Filesystem(new LocalFilesystemAdapter(cachePath())));
38+
return new FlysystemDriverInterface(new Filesystem(new LocalFilesystemAdapter(cachePath())));
3939
}
4040

4141
public function cacheTTLInSeconds(): int

0 commit comments

Comments
 (0)