-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHasCaching.php
More file actions
119 lines (98 loc) · 3.19 KB
/
HasCaching.php
File metadata and controls
119 lines (98 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
declare(strict_types=1);
namespace Saloon\CachePlugin\Traits;
use Saloon\Enums\Method;
use Saloon\Enums\PipeOrder;
use Saloon\Http\PendingRequest;
use Saloon\CachePlugin\Contracts\Cacheable;
use Saloon\CachePlugin\Exceptions\HasCachingException;
use Saloon\CachePlugin\Http\Middleware\CacheMiddleware;
trait HasCaching
{
/**
* Is caching enabled?
*/
protected bool $cachingEnabled = true;
/**
* Should the existing cache be invalidated?
*/
protected bool $invalidateCache = false;
/**
* Boot the "HasCaching" plugin
*
* @throws \Saloon\CachePlugin\Exceptions\HasCachingException
* @throws \Saloon\Exceptions\DuplicatePipeNameException
*/
public function bootHasCaching(PendingRequest $pendingRequest): void
{
$request = $pendingRequest->getRequest();
$connector = $pendingRequest->getConnector();
if (! $request instanceof Cacheable && ! $connector instanceof Cacheable) {
throw new HasCachingException(sprintf('Your connector or request must implement %s to use the HasCaching plugin', Cacheable::class));
}
if ($this->cachingEnabled === false) {
return;
}
if (! in_array($pendingRequest->getMethod(), $this->getCacheableMethods(), true)) {
return;
}
$cacheDriver = $request instanceof Cacheable
? $request->resolveCacheDriver()
: $connector->resolveCacheDriver();
// Register a request middleware which wil handle the caching
// and recording of real responses for caching.
$pendingRequest->middleware()->onRequest(function (PendingRequest $middlewarePendingRequest) use ($cacheDriver) {
// We'll call the cache middleware invokable class with the $middlewarePendingRequest
// because this $pendingRequest has everything loaded, unlike the instance that
// the plugin is provided. This allows us to have access to body and merged
// properties.
return call_user_func(new CacheMiddleware($cacheDriver, $this->cacheKey($middlewarePendingRequest), $this->invalidateCache), $middlewarePendingRequest);
}, order: PipeOrder::FIRST);
}
/**
* Define a custom cache key
*/
protected function cacheKey(PendingRequest $pendingRequest): ?string
{
return null;
}
/**
* Enable caching for the request.
*
* @return $this
*/
public function enableCaching(): static
{
$this->cachingEnabled = true;
return $this;
}
/**
* Disable caching for the request.
*
* @return $this
*/
public function disableCaching(): static
{
$this->cachingEnabled = false;
return $this;
}
/**
* Invalidate the current cache and refresh the cache.
*
* @return $this
*/
public function invalidateCache(): static
{
$this->invalidateCache = true;
return $this;
}
/**
* Define the cacheable methods that can be used
*
* @return array<\Saloon\Enums\Method>
*/
protected function getCacheableMethods(): array
{
return [Method::GET, Method::OPTIONS];
}
}