PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.18.6
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.18.6
1.19.8 1.19.7 1.19.6 1.19.5 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.12.0 1.13.0 1.14.0 1.15.0 1.15.1 1.15.2 1.15.3 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.16.6 1.16.7 1.16.8 1.17.0 1.17.6 1.17.7 1.17.8 1.17.9 1.18.0 1.18.1 1.18.2 1.18.3 1.18.4 1.18.5 1.18.6 1.18.7 1.18.8 1.18.9 1.19.0 1.19.1 1.19.2 1.19.3 1.19.4 1.3.19 1.3.20 1.4.0 1.4.1 1.5.0 1.5.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.16 1.5.17 1.5.18 1.5.19 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.8.1 1.8.3 1.9.0 1.9.1 1.9.2
nitropack / nitropack-sdk / NitroPack / SDK / Api / Cache.php
nitropack / nitropack-sdk / NitroPack / SDK / Api Last commit date
AdditionalDomains.php 1 year ago Base.php 8 months ago Cache.php 1 year ago ExcludedUrls.php 1 year ago Excludes.php 1 year ago Integration.php 1 year ago RemoteConfigFetcher.php 1 year ago RequestMaker.php 1 year ago Response.php 1 year ago ResponseStatus.php 1 year ago SafeMode.php 1 year ago SecureRequestMaker.php 1 year ago SignedBase.php 1 year ago Stats.php 1 year ago Tagger.php 1 year ago Url.php 1 year ago VariationCookie.php 1 year ago Varnish.php 1 year ago Warmup.php 1 year ago Webhook.php 1 year ago
Cache.php
381 lines
1 <?php
2 namespace NitroPack\SDK\Api;
3
4 use \NitroPack\SDK\NitroPack;
5 use \NitroPack\SDK\ServiceDownException;
6 use \NitroPack\HttpClient\HttpClientMulti;
7 use \NitroPack\HttpClient\Exceptions\SocketReadTimedOutException;
8
9 class Cache extends SignedBase {
10 protected $secret;
11
12 public function __construct($siteId, $siteSecret) {
13 parent::__construct($siteId, $siteSecret);
14 $this->secret = $siteSecret;
15 }
16
17 public function get($url, $userAgent, $cookies, $isAjax, $layout = 'default', $remoteAddr = NULL, $referrer = NULL) {
18 $this->isBacklogEnabled = false;
19 $path = 'cache/get/' . $this->siteId . '/' . $layout;
20 $remoteAddr = $remoteAddr ? $remoteAddr : NitroPack::getRemoteAddr();
21
22 $headers = array(
23 'X-Nitro-Url' => $url,
24 'X-Nitro-Visitor-Addr' => $remoteAddr,
25 'User-Agent' => $userAgent
26 );
27
28 $customCachePrefix = NitroPack::getCustomCachePrefix();
29 if ($customCachePrefix) {
30 $headers['X-Nitro-Cache-Prefix'] = $customCachePrefix;
31 }
32
33 if ($isAjax) {
34 $headers['X-Nitro-Ajax'] = 1;
35 }
36
37 if ($referrer) {
38 $headers['Referer'] = $referrer;
39 }
40
41 if ($this->isCacheWarmupRequest()) {
42 $headers['X-Nitro-Priority'] = 'LOW';
43 }
44
45 if (!empty($this->nitropack->getConfig()->LoopbackRequests)) {
46 $headers['X-Nitro-Loopback'] = '1';
47 }
48
49 $httpResponse = $this->makeRequest($path, $headers, $cookies);
50 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
51 $body = $httpResponse->getBody();
52 $response = new Response($status, $body);
53 return $response;
54 }
55
56 public function getMulti($urls, $userAgent, $cookies, $isAjax, $layout = 'default', $remoteAddr = NULL, $referrer = NULL) {
57 $this->isBacklogEnabled = false;
58 $path = 'cache/get/' . $this->siteId . '/' . $layout;
59 $remoteAddr = $remoteAddr ? $remoteAddr : NitroPack::getRemoteAddr();
60
61 $headers = array(
62 'X-Nitro-Visitor-Addr' => $remoteAddr,
63 'User-Agent' => $userAgent
64 );
65
66 $customCachePrefix = NitroPack::getCustomCachePrefix();
67 if ($customCachePrefix) {
68 $headers['X-Nitro-Cache-Prefix'] = $customCachePrefix;
69 }
70
71 if ($isAjax) {
72 $headers['X-Nitro-Ajax'] = 1;
73 }
74
75 if ($referrer) {
76 $headers['Referer'] = $referrer;
77 }
78
79 if ($this->isCacheWarmupRequest()) {
80 $headers['X-Nitro-Priority'] = 'LOW';
81 }
82
83 if (!empty($this->nitropack->getConfig()->LoopbackRequests)) {
84 $headers['X-Nitro-Loopback'] = '1';
85 }
86
87 $chunkSize = min(5, count($urls));
88 $cache = $this;
89 $retries = new \SplObjectStorage();
90 $httpMulti = new HttpClientMulti();
91
92 $httpMulti->onSuccess(function($client) use ($path, &$urls, $httpMulti, $cache, $headers, $cookies) {
93 if ($urls) {
94 $_url = array_shift($urls);
95 $headers['X-Nitro-Url'] = $_url;
96
97 $httpClient = $cache->makeRequestAsync($path, $headers, $cookies);
98 $httpMulti->push($httpClient);
99 }
100 });
101
102 $httpMulti->onError(function($client, $exception) use ($httpMulti, $retries) {
103 if ($exception instanceof SocketReadTimedOutException) {
104 return;
105 }
106
107 if (!$retries->offsetExists($client)) {
108 $clientRetries = 0;
109 } else {
110 $clientRetries = $retries->offsetGet($client);
111 }
112
113 if ($clientRetries < 5) {
114 $retries->offsetSet($client, $clientRetries + 1);
115 $client->replay();
116 $httpMulti->push($client);
117 }
118 });
119
120 while ($urls && count($httpMulti->getClients()) < $chunkSize) {
121 $_url = array_shift($urls);
122 $headers['X-Nitro-Url'] = $_url;
123
124 try {
125 $httpClient = $this->makeRequestAsync($path, $headers, $cookies);
126 } catch (ServiceDownException $e) {
127 continue;
128 }
129
130 $httpMulti->push($httpClient);
131 }
132
133 $res = $httpMulti->readAll(); // This blocks untill all requests have finished
134
135 foreach ($res[1] as $failedRequest) {
136 $exception = $failedRequest[1];
137
138 if ($exception instanceof SocketReadTimedOutException) {
139 continue; // Ignore read timeouts
140 } else {
141 throw $exception;
142 }
143 }
144
145 $responses = [];
146 foreach ($res[0] as $httpResponse) {
147 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
148 if ($status !== ResponseStatus::OK) {
149 $this->throwException($httpResponse, 'Error while getting cache: %s');
150 }
151
152 $url = !empty($httpResponse->request_headers['x-nitro-url']) ? $httpResponse->request_headers['x-nitro-url'] : null;
153 if (!$url) {
154 $this->throwException($httpResponse, 'Error while getting cache, url header missing: %s');
155 }
156
157 $body = $httpResponse->getBody();
158 $responses[$url] = new Response($status, $body);
159 }
160
161 return $responses;
162 }
163
164 public function getLastPurge() {
165 $this->isBacklogEnabled = false;
166 $path = 'cache/getlastpurge/' . $this->siteId;
167
168 $httpResponse = $this->makeRequest($path);
169 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
170 switch ($status) {
171 case ResponseStatus::OK:
172 return json_decode($httpResponse->getBody(), true);
173 default:
174 $this->throwException($httpResponse, 'Error while getting information about the last cache purge: %s');
175 }
176 }
177
178 public function purge($url = NULL, $pagecacheOnly = false, $reason = NULL, $lightPurge = false) {
179 $this->isBacklogEnabled = true;
180 $path = 'cache/purge/' . $this->siteId;
181
182 if (is_array($url)) {
183 $chunkSize = min(25, (int)ceil(count($url) * 0.2)); // 20% of the number of URLs up to 25 simultaneous requests
184 $requests = array();
185 $cache = $this;
186 $retries = new \SplObjectStorage();
187 $httpMulti = new HttpClientMulti();
188
189 $httpMulti->onSuccess(function($client) use ($path, &$url, &$requests, $httpMulti, $cache, $reason, $lightPurge) {
190 if ($client->getStatusCode() >= 500 && !empty($client->backlogEntry)) {
191 $this->addToBacklog($client->backlogEntry);
192 $this->nitropack && $this->nitropack->setHealthStatus(HealthStatus::UNDER_THE_WEATHER);
193 }
194
195 if ($url) {
196 $_url = array_shift($url);
197 $params = array();
198 $params["url"] = $_url;
199
200 if ($reason) {
201 $params["reason"] = $reason;
202 }
203 if ($lightPurge) {
204 $params["light_purge"] = $lightPurge;
205 }
206 $httpClient = $cache->makeRequestAsync($path, array(), array(), 'POST', $params);
207 $httpMulti->push($httpClient);
208 $requests[] = $httpClient;
209 }
210 });
211
212 $httpMulti->onError(function($client, $exception) use ($httpMulti, $retries) {
213 if ($exception instanceof SocketReadTimedOutException) {
214 if (!empty($client->backlogEntry)) {
215 $this->addToBacklog($client->backlogEntry);
216 $this->nitropack && $this->nitropack->setHealthStatus(HealthStatus::SICK);
217 }
218 return;
219 }
220
221 if (!$retries->offsetExists($client)) {
222 $clientRetries = 0;
223 } else {
224 $clientRetries = $retries->offsetGet($client);
225 }
226
227 if ($clientRetries < 5) {
228 $retries->offsetSet($client, $clientRetries + 1);
229 $client->replay();
230 $httpMulti->push($client);
231 } else {
232 if (!empty($client->backlogEntry)) {
233 $this->addToBacklog($client->backlogEntry);
234 }
235 }
236 });
237
238 while ($url && count($httpMulti->getClients()) < $chunkSize) {
239 $_url = array_shift($url);
240 $params = array();
241 $params["url"] = $_url;
242
243 if ($reason) {
244 $params["reason"] = $reason;
245 }
246 if ($lightPurge) {
247 $params["light_purge"] = $lightPurge;
248 }
249
250 try {
251 $httpClient = $this->makeRequestAsync($path, array(), array(), 'POST', $params);
252 } catch (ServiceDownException $e) {
253 continue;
254 }
255
256 $httpMulti->push($httpClient);
257 $requests[] = $httpClient;
258 }
259
260 $res = $httpMulti->readAll(); // This blocks untill all requests have finished
261
262 foreach ($res[1] as $failedRequest) {
263 $exception = $failedRequest[1];
264
265 if ($exception instanceof SocketReadTimedOutException) {
266 continue; // Ignore read timeouts
267 } else {
268 throw $exception;
269 }
270 }
271
272 foreach ($res[0] as $httpResponse) {
273 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
274 if ($status !== ResponseStatus::OK) {
275 $this->throwException($httpResponse, 'Error while purging cache: %s');
276 }
277 }
278
279 return true;
280 } else {
281 $params = array();
282 if ($url) {
283 $params["url"] = $url;
284 }
285
286 if ($pagecacheOnly) {
287 $params["pagecache_only"] = true;
288 }
289
290 if ($reason) {
291 $params["reason"] = $reason;
292 }
293 if ($lightPurge) {
294 $params["light_purge"] = $lightPurge;
295 }
296
297 try {
298 $httpResponse = $this->makeRequest($path, array(), array(), 'POST', $params);
299 } catch (SocketReadTimedOutException $e) {
300 return true;
301 }
302
303 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
304 switch ($status) {
305 case ResponseStatus::OK:
306 return true;
307 default:
308 $this->throwException($httpResponse, 'Error while purging cache: %s');
309 }
310 }
311
312 return false;
313 }
314
315 public function purgeByTag($tag, $reason = NULL, $lightPurge = false) {
316 $this->isBacklogEnabled = true;
317 $path = 'cache/purge/' . $this->siteId;
318
319 $params = array();
320
321 $params["tag"] = $tag;
322
323 if ($reason) {
324 $params["reason"] = $reason;
325 }
326
327 if ($lightPurge) {
328 $params["light_purge"] = $lightPurge;
329 }
330
331 try {
332 $httpResponse = $this->makeRequest($path, array(), array(), 'POST', $params);
333 } catch (SocketReadTimedOutException $e) {
334 throw new \RuntimeException(sprintf('Timeout while purging cache by tag: %s', $tag));
335 }
336
337 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
338 switch ($status) {
339 case ResponseStatus::OK:
340 $body = json_decode($httpResponse->getBody(), true);
341
342 return isset($body['purged_urls']) ? $body['purged_urls'] : array();
343 default:
344 $this->throwException($httpResponse, 'Error while purging cache by tag: %s');
345 }
346
347 return array();
348 }
349
350 public function enableCartCache() {
351 $path = 'cache/enablecartcache/' . $this->siteId;
352
353 $httpResponse = $this->makeRequest($path, array(), array(), 'POST');
354
355 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
356 switch ($status) {
357 case ResponseStatus::OK:
358 return true;
359 default:
360 $this->throwException($httpResponse, 'Error while enabling cart cache: %s');
361 }
362 }
363
364 public function disableCartCache() {
365 $path = 'cache/disablecartcache/' . $this->siteId;
366
367 $httpResponse = $this->makeRequest($path, array(), array(), 'POST');
368
369 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
370 switch ($status) {
371 case ResponseStatus::OK:
372 return true;
373 default:
374 $this->throwException($httpResponse, 'Error while disabling cart cache: %s');
375 }
376 }
377
378 private function isCacheWarmupRequest() {
379 return isset($_SERVER['HTTP_X_NITRO_WARMUP']);
380 }
381 }