-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHasCaching.php
More file actions
155 lines (128 loc) · 4.42 KB
/
HasCaching.php
File metadata and controls
155 lines (128 loc) · 4.42 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
namespace Saloon\CachePlugin\Traits;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Connector;
use Saloon\Enums\PipeOrder;
use Saloon\Http\PendingRequest;
use Saloon\CachePlugin\Contracts\Cacheable;
use Saloon\CachePlugin\Helpers\CacheKeyHelper;
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();
$cacheExpiryInSeconds = $request instanceof Cacheable
? $request->cacheExpiryInSeconds()
: $connector->cacheExpiryInSeconds();
// Register a request middleware which wil handle the caching
// and recording of real responses for caching.
$pendingRequest->middleware()->onRequest(function (PendingRequest $middlewarePendingRequest) use ($cacheDriver, $cacheExpiryInSeconds) {
// 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, $cacheExpiryInSeconds, $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;
}
/**
* Delete the cached response without sending a request.
*
* When used on a Request, pass the Connector.
* When used on a Connector, pass the Request.
*
* @throws \JsonException
*/
public function deleteCache(Connector|Request $counterpart): void
{
if ($this instanceof Request) {
$pendingRequest = $counterpart->createPendingRequest($this);
} else {
$pendingRequest = $this->createPendingRequest($counterpart);
}
$request = $pendingRequest->getRequest();
$connector = $pendingRequest->getConnector();
$cacheDriver = $request instanceof Cacheable
? $request->resolveCacheDriver()
: $connector->resolveCacheDriver();
$rawKey = $this->cacheKey($pendingRequest) ?? CacheKeyHelper::create($pendingRequest);
$cacheKey = hash('sha256', $rawKey);
$cacheDriver->delete($cacheKey);
}
/**
* Define the cacheable methods that can be used
*
* @return array<\Saloon\Enums\Method>
*/
protected function getCacheableMethods(): array
{
return [Method::GET, Method::OPTIONS];
}
}