PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.7.0
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.7.0
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 3 years ago Base.php 3 years ago Cache.php 3 years ago ExcludedUrls.php 3 years ago Integration.php 6 years ago RemoteConfigFetcher.php 4 years ago RequestMaker.php 5 years ago Response.php 6 years ago ResponseStatus.php 6 years ago SafeMode.php 3 years ago SecureRequestMaker.php 5 years ago SignedBase.php 3 years ago Stats.php 6 years ago Tagger.php 3 years ago Url.php 6 years ago VariationCookie.php 6 years ago Varnish.php 3 years ago Warmup.php 6 years ago Webhook.php 6 years ago
Cache.php
273 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 getLastPurge() {
57 $this->isBacklogEnabled = false;
58 $path = 'cache/getlastpurge/' . $this->siteId;
59
60 $httpResponse = $this->makeRequest($path);
61 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
62 switch ($status) {
63 case ResponseStatus::OK:
64 return json_decode($httpResponse->getBody(), true);
65 default:
66 $this->throwException($httpResponse, 'Error while getting information about the last cache purge: %s');
67 }
68 }
69
70 public function purge($url = NULL, $pagecacheOnly = false, $reason = NULL, $lightPurge = false) {
71 $this->isBacklogEnabled = true;
72 $path = 'cache/purge/' . $this->siteId;
73
74 if (is_array($url)) {
75 $chunkSize = min(25, (int)ceil(count($url) * 0.2)); // 20% of the number of URLs up to 25 simultaneous requests
76 $requests = array();
77 $cache = $this;
78 $retries = new \SplObjectStorage();
79 $httpMulti = new HttpClientMulti();
80
81 $httpMulti->onSuccess(function($client) use ($path, &$url, &$requests, $httpMulti, $cache, $reason, $lightPurge) {
82 if ($client->getStatusCode() >= 500 && !empty($client->backlogEntry)) {
83 $this->addToBacklog($client->backlogEntry);
84 $this->nitropack && $this->nitropack->setHealthStatus(HealthStatus::UNDER_THE_WEATHER);
85 }
86
87 if ($url) {
88 $_url = array_shift($url);
89 $params = array();
90 $params["url"] = $_url;
91
92 if ($reason) {
93 $params["reason"] = $reason;
94 }
95 if ($lightPurge) {
96 $params["light_purge"] = $lightPurge;
97 }
98 $httpClient = $cache->makeRequestAsync($path, array(), array(), 'POST', $params);
99 $httpMulti->push($httpClient);
100 $requests[] = $httpClient;
101 }
102 });
103
104 $httpMulti->onError(function($client, $exception) use ($httpMulti, $retries) {
105 if ($exception instanceof SocketReadTimedOutException) {
106 if (!empty($client->backlogEntry)) {
107 $this->addToBacklog($client->backlogEntry);
108 $this->nitropack && $this->nitropack->setHealthStatus(HealthStatus::SICK);
109 }
110 return;
111 }
112
113 if (!$retries->offsetExists($client)) {
114 $clientRetries = 0;
115 } else {
116 $clientRetries = $retries->offsetGet($client);
117 }
118
119 if ($clientRetries < 5) {
120 $retries->offsetSet($client, $clientRetries + 1);
121 $client->replay();
122 $httpMulti->push($client);
123 } else {
124 if (!empty($client->backlogEntry)) {
125 $this->addToBacklog($client->backlogEntry);
126 }
127 }
128 });
129
130 while ($url && count($httpMulti->getClients()) < $chunkSize) {
131 $_url = array_shift($url);
132 $params = array();
133 $params["url"] = $_url;
134
135 if ($reason) {
136 $params["reason"] = $reason;
137 }
138 if ($lightPurge) {
139 $params["light_purge"] = $lightPurge;
140 }
141
142 try {
143 $httpClient = $this->makeRequestAsync($path, array(), array(), 'POST', $params);
144 } catch (ServiceDownException $e) {
145 continue;
146 }
147
148 $httpMulti->push($httpClient);
149 $requests[] = $httpClient;
150 }
151
152 $res = $httpMulti->readAll(); // This blocks untill all requests have finished
153
154 foreach ($res[1] as $failedRequest) {
155 $exception = $failedRequest[1];
156
157 if ($exception instanceof SocketReadTimedOutException) {
158 continue; // Ignore read timeouts
159 } else {
160 throw $exception;
161 }
162 }
163
164 foreach ($res[0] as $httpResponse) {
165 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
166 if ($status !== ResponseStatus::OK) {
167 $this->throwException($httpResponse, 'Error while purging cache: %s');
168 }
169 }
170
171 return true;
172 } else {
173 $params = array();
174 if ($url) {
175 $params["url"] = $url;
176 }
177
178 if ($pagecacheOnly) {
179 $params["pagecache_only"] = true;
180 }
181
182 if ($reason) {
183 $params["reason"] = $reason;
184 }
185 if ($lightPurge) {
186 $params["light_purge"] = $lightPurge;
187 }
188
189 try {
190 $httpResponse = $this->makeRequest($path, array(), array(), 'POST', $params);
191 } catch (SocketReadTimedOutException $e) {
192 return true;
193 }
194
195 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
196 switch ($status) {
197 case ResponseStatus::OK:
198 return true;
199 default:
200 $this->throwException($httpResponse, 'Error while purging cache: %s');
201 }
202 }
203
204 return false;
205 }
206
207 public function purgeByTag($tag, $reason = NULL, $lightPurge = false) {
208 $this->isBacklogEnabled = true;
209 $path = 'cache/purge/' . $this->siteId;
210
211 $params = array();
212
213 $params["tag"] = $tag;
214
215 if ($reason) {
216 $params["reason"] = $reason;
217 }
218
219 if ($lightPurge) {
220 $params["light_purge"] = $lightPurge;
221 }
222
223 try {
224 $httpResponse = $this->makeRequest($path, array(), array(), 'POST', $params);
225 } catch (SocketReadTimedOutException $e) {
226 throw new \RuntimeException(sprintf('Timeout while purging cache by tag: %s', $tag));
227 }
228
229 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
230 switch ($status) {
231 case ResponseStatus::OK:
232 $body = json_decode($httpResponse->getBody(), true);
233
234 return isset($body['purged_urls']) ? $body['purged_urls'] : array();
235 default:
236 $this->throwException($httpResponse, 'Error while purging cache by tag: %s');
237 }
238
239 return array();
240 }
241
242 public function enableCartCache() {
243 $path = 'cache/enablecartcache/' . $this->siteId;
244
245 $httpResponse = $this->makeRequest($path, array(), array(), 'POST');
246
247 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
248 switch ($status) {
249 case ResponseStatus::OK:
250 return true;
251 default:
252 $this->throwException($httpResponse, 'Error while enabling cart cache: %s');
253 }
254 }
255
256 public function disableCartCache() {
257 $path = 'cache/disablecartcache/' . $this->siteId;
258
259 $httpResponse = $this->makeRequest($path, array(), array(), 'POST');
260
261 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
262 switch ($status) {
263 case ResponseStatus::OK:
264 return true;
265 default:
266 $this->throwException($httpResponse, 'Error while disabling cart cache: %s');
267 }
268 }
269
270 private function isCacheWarmupRequest() {
271 return isset($_SERVER['HTTP_X_NITRO_WARMUP']);
272 }
273 }