-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathCacheInvalidator.php
291 lines (256 loc) · 9.25 KB
/
CacheInvalidator.php
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/*
* This file is part of the FOSHttpCache package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\HttpCache;
use FOS\HttpCache\Exception\ExceptionCollection;
use FOS\HttpCache\Exception\InvalidArgumentException;
use FOS\HttpCache\Exception\ProxyResponseException;
use FOS\HttpCache\Exception\ProxyUnreachableException;
use FOS\HttpCache\Exception\UnsupportedProxyOperationException;
use FOS\HttpCache\ProxyClient\Invalidation\BanCapable;
use FOS\HttpCache\ProxyClient\Invalidation\ClearCapable;
use FOS\HttpCache\ProxyClient\Invalidation\PurgeCapable;
use FOS\HttpCache\ProxyClient\Invalidation\RefreshCapable;
use FOS\HttpCache\ProxyClient\Invalidation\TagCapable;
use FOS\HttpCache\ProxyClient\ProxyClient;
use FOS\HttpCache\ProxyClient\Symfony;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Toflar\Psr6HttpCacheStore\Psr6Store;
/**
* Manages HTTP cache invalidation.
*
* @author David de Boer <[email protected]>
* @author David Buchmann <[email protected]>
* @author André Rømcke <[email protected]>
*/
class CacheInvalidator
{
/**
* Value to check support of invalidatePath operation.
*/
public const PATH = 'path';
/**
* Value to check support of refreshPath operation.
*/
public const REFRESH = 'refresh';
/**
* Value to check support of invalidate and invalidateRegex operations.
*/
public const INVALIDATE = 'invalidate';
/**
* Value to check support of invalidateTags operation.
*/
public const TAGS = 'tags';
/**
* Value to check support of clearCache operation.
*/
public const CLEAR = 'clear';
private EventDispatcherInterface $eventDispatcher;
public function __construct(
private readonly ProxyClient $cache,
) {
}
/**
* Check whether this invalidator instance supports the specified
* operation.
*
* Support for PATH means invalidatePath will work, REFRESH means
* refreshPath works, TAGS means that invalidateTags works and
* INVALIDATE is for the invalidate and invalidateRegex methods.
*
* @param string $operation one of the class constants
*
* @throws InvalidArgumentException
*/
public function supports(string $operation): bool
{
switch ($operation) {
case self::PATH:
return $this->cache instanceof PurgeCapable;
case self::REFRESH:
return $this->cache instanceof RefreshCapable;
case self::INVALIDATE:
return $this->cache instanceof BanCapable;
case self::TAGS:
$supports = $this->cache instanceof TagCapable;
if ($supports && $this->cache instanceof Symfony) {
return class_exists(Psr6Store::class);
}
return $supports;
case self::CLEAR:
return $this->cache instanceof ClearCapable;
default:
throw new InvalidArgumentException('Unknown operation '.$operation);
}
}
/**
* Set event dispatcher - may only be called once.
*
* @throws \Exception when trying to override the event dispatcher
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
{
if (isset($this->eventDispatcher)) {
// if you want to set a custom event dispatcher, do so right after instantiating
// the invalidator.
throw new \Exception('You may not change the event dispatcher once it is set.');
}
$this->eventDispatcher = $eventDispatcher;
}
/**
* Get the event dispatcher used by the cache invalidator.
*/
public function getEventDispatcher(): EventDispatcherInterface
{
if (!isset($this->eventDispatcher)) {
$this->eventDispatcher = new EventDispatcher();
}
return $this->eventDispatcher;
}
/**
* Invalidate a path or URL.
*
* @param string $path Path or URL
* @param array<string, string> $headers HTTP headers (optional)
*
* @throws UnsupportedProxyOperationException
*/
public function invalidatePath(string $path, array $headers = []): static
{
if (!$this->cache instanceof PurgeCapable) {
throw UnsupportedProxyOperationException::cacheDoesNotImplement('PURGE');
}
$this->cache->purge($path, $headers);
return $this;
}
/**
* Refresh a path or URL.
*
* @param string $path Path or URL
* @param array<string, string> $headers HTTP headers (optional)
*
* @see RefreshCapable::refresh()
*
* @throws UnsupportedProxyOperationException
*/
public function refreshPath(string $path, array $headers = []): static
{
if (!$this->cache instanceof RefreshCapable) {
throw UnsupportedProxyOperationException::cacheDoesNotImplement('REFRESH');
}
$this->cache->refresh($path, $headers);
return $this;
}
/**
* Invalidate all cached objects matching the provided HTTP headers.
*
* Each header is a a POSIX regular expression, for example
* ['X-Host' => '^(www\.)?(this|that)\.com$']
*
* @see BanCapable::ban()
*
* @param array<string, string> $headers HTTP headers that path must match to be banned
*
* @throws UnsupportedProxyOperationException If HTTP cache does not support BAN requests
*/
public function invalidate(array $headers): static
{
if (!$this->cache instanceof BanCapable) {
throw UnsupportedProxyOperationException::cacheDoesNotImplement('BAN');
}
$this->cache->ban($headers);
return $this;
}
/**
* Remove/Expire cache objects based on cache tags.
*
* @see TagCapable::tags()
*
* @param string[] $tags Tags that should be removed/expired from the cache. An empty tag list is ignored.
*
* @throws UnsupportedProxyOperationException If HTTP cache does not support Tags invalidation
*/
public function invalidateTags(array $tags): static
{
if (!$this->cache instanceof TagCapable) {
throw UnsupportedProxyOperationException::cacheDoesNotImplement('Tags');
}
if (!$tags) {
return $this;
}
$this->cache->invalidateTags($tags);
return $this;
}
/**
* Invalidate URLs based on a regular expression for the URI, an optional
* content type and optional limit to certain hosts.
*
* The hosts parameter can either be a regular expression, e.g.
* '^(www\.)?(this|that)\.com$' or an array of exact host names, e.g.
* ['example.com', 'other.net']. If the parameter is empty, all hosts
* are matched.
*
* @param string $path Regular expression pattern for URI to
* invalidate
* @param string|null $contentType Regular expression pattern for the content
* type to limit banning, for instance 'text'
* @param array|string|null $hosts Regular expression of a host name or list of
* exact host names to limit banning
*
* @throws UnsupportedProxyOperationException If HTTP cache does not support BAN requests
*
*@see BanCapable::banPath()
*/
public function invalidateRegex(string $path, ?string $contentType = null, array|string|null $hosts = null): static
{
if (!$this->cache instanceof BanCapable) {
throw UnsupportedProxyOperationException::cacheDoesNotImplement('BAN');
}
$this->cache->banPath($path, $contentType, $hosts);
return $this;
}
/**
* Clear the cache completely.
*
* @throws UnsupportedProxyOperationException if HTTP cache does not support clearing the cache completely
*/
public function clearCache(): static
{
if (!$this->cache instanceof ClearCapable) {
throw UnsupportedProxyOperationException::cacheDoesNotImplement('CLEAR');
}
$this->cache->clear();
return $this;
}
/**
* Send all pending invalidation requests.
*
* @return int the number of cache invalidations performed per caching server
*
* @throws ExceptionCollection if any errors occurred during flush
*/
public function flush(): int
{
try {
return $this->cache->flush();
} catch (ExceptionCollection $exceptions) {
foreach ($exceptions as $exception) {
$event = new Event();
$event->setException($exception);
if ($exception instanceof ProxyResponseException) {
$this->getEventDispatcher()->dispatch($event, Events::PROXY_RESPONSE_ERROR);
} elseif ($exception instanceof ProxyUnreachableException) {
$this->getEventDispatcher()->dispatch($event, Events::PROXY_UNREACHABLE_ERROR);
}
}
throw $exceptions;
}
}
}