PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.5.8
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.5.8
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
Base.php 5 years ago Cache.php 5 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 5 years ago SecureRequestMaker.php 5 years ago SignedBase.php 5 years ago Stats.php 6 years ago Tagger.php 6 years ago Url.php 6 years ago VariationCookie.php 6 years ago Warmup.php 6 years ago Webhook.php 6 years ago
Cache.php
227 lines
1 <?php
2 namespace NitroPack\SDK\Api;
3
4 use \NitroPack\SDK\NitroPack;
5 use \NitroPack\SDK\ServiceDownException;
6
7 class Cache extends SignedBase {
8 protected $secret;
9
10 public function __construct($siteId, $siteSecret) {
11 parent::__construct($siteId, $siteSecret);
12 $this->secret = $siteSecret;
13 }
14
15 public function get($url, $userAgent, $cookies, $isAjax, $layout = 'default', $remoteAddr = NULL) {
16 $this->isBacklogEnabled = false;
17 $path = 'cache/get/' . $this->siteId . '/' . $layout;
18 $remoteAddr = $remoteAddr ? $remoteAddr : NitroPack::getRemoteAddr();
19 $headers = array(
20 'X-Nitro-Url' => $url,
21 'X-Nitro-Visitor-Addr' => $remoteAddr,
22 'User-Agent' => $userAgent
23 );
24
25 $customCachePrefix = NitroPack::getCustomCachePrefix();
26 if ($customCachePrefix) {
27 $headers['X-Nitro-Cache-Prefix'] = $customCachePrefix;
28 }
29
30 if ($isAjax) {
31 $headers['X-Nitro-Ajax'] = 1;
32 }
33
34 if ($this->isCacheWarmupRequest()) {
35 $headers['X-Nitro-Priority'] = 'LOW';
36 }
37
38 if (!empty($this->nitropack->getConfig()->LoopbackRequests)) {
39 $headers['X-Nitro-Loopback'] = '1';
40 }
41
42 $httpResponse = $this->makeRequest($path, $headers, $cookies);
43 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
44 $body = $httpResponse->getBody();
45 $response = new Response($status, $body);
46 return $response;
47 }
48
49 public function getLastPurge() {
50 $this->isBacklogEnabled = false;
51 $path = 'cache/getlastpurge/' . $this->siteId;
52
53 $httpResponse = $this->makeRequest($path);
54 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
55 switch ($status) {
56 case ResponseStatus::OK:
57 return json_decode($httpResponse->getBody(), true);
58 default:
59 $this->throwException($httpResponse, 'Error while getting information about the last cache purge: %s');
60 }
61 }
62
63 public function purge($url = NULL, $pagecacheOnly = false, $reason = NULL) {
64 $this->isBacklogEnabled = true;
65 $path = 'cache/purge/' . $this->siteId;
66
67 if (is_array($url)) {
68 $chunkSize = min(25, (int)ceil(count($url) * 0.2)); // 20% of the number of URLs up to 25 simultaneous requests
69 $requests = array();
70 $cache = $this;
71 $retries = new \SplObjectStorage();
72 $httpMulti = new \NitroPack\HttpClientMulti();
73
74 $httpMulti->onSuccess(function($client) use ($path, &$url, &$requests, $httpMulti, $cache, $reason) {
75 if ($client->getStatusCode() >= 500 && !empty($client->backlogEntry)) {
76 $this->addToBacklog($client->backlogEntry);
77 $this->nitropack && $this->nitropack->setHealthStatus(HealthStatus::UNDER_THE_WEATHER);
78 }
79
80 if ($url) {
81 $_url = array_shift($url);
82 $params = array();
83 $params["url"] = $_url;
84
85 if ($reason) {
86 $params["reason"] = $reason;
87 }
88
89 $httpClient = $cache->makeRequestAsync($path, array(), array(), 'POST', $params);
90 $httpMulti->push($httpClient);
91 $requests[] = $httpClient;
92 }
93 });
94
95 $httpMulti->onError(function($client, $exception) use ($httpMulti, $retries) {
96 if ($exception instanceof \NitroPack\SocketReadTimedOutException) {
97 if (!empty($client->backlogEntry)) {
98 $this->addToBacklog($client->backlogEntry);
99 $this->nitropack && $this->nitropack->setHealthStatus(HealthStatus::SICK);
100 }
101 return;
102 }
103
104 if (!$retries->offsetExists($client)) {
105 $clientRetries = 0;
106 } else {
107 $clientRetries = $retries->offsetGet($client);
108 }
109
110 if ($clientRetries < 5) {
111 $retries->offsetSet($client, $clientRetries + 1);
112 $client->replay();
113 $httpMulti->push($client);
114 } else {
115 if (!empty($client->backlogEntry)) {
116 $this->addToBacklog($client->backlogEntry);
117 }
118 }
119 });
120
121 while ($url && count($httpMulti->getClients()) < $chunkSize) {
122 $_url = array_shift($url);
123 $params = array();
124 $params["url"] = $_url;
125
126 if ($reason) {
127 $params["reason"] = $reason;
128 }
129
130 try {
131 $httpClient = $this->makeRequestAsync($path, array(), array(), 'POST', $params);
132 } catch (ServiceDownException $e) {
133 continue;
134 }
135
136 $httpMulti->push($httpClient);
137 $requests[] = $httpClient;
138 }
139
140 $res = $httpMulti->readAll(); // This blocks untill all requests have finished
141
142 foreach ($res[1] as $failedRequest) {
143 $exception = $failedRequest[1];
144
145 if ($exception instanceof \NitroPack\SocketReadTimedOutException) {
146 continue; // Ignore read timeouts
147 } else {
148 throw $exception;
149 }
150 }
151
152 foreach ($res[0] as $httpResponse) {
153 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
154 if ($status !== ResponseStatus::OK) {
155 $this->throwException($httpResponse, 'Error while purging cache: %s');
156 }
157 }
158
159 return true;
160 } else {
161 $params = array();
162 if ($url) {
163 $params["url"] = $url;
164 }
165
166 if ($pagecacheOnly) {
167 $params["pagecache_only"] = true;
168 }
169
170 if ($reason) {
171 $params["reason"] = $reason;
172 }
173
174 try {
175 $httpResponse = $this->makeRequest($path, array(), array(), 'POST', $params);
176 } catch (\NitroPack\SocketReadTimedOutException $e) {
177 return true;
178 }
179
180 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
181 switch ($status) {
182 case ResponseStatus::OK:
183 return true;
184 default:
185 $this->throwException($httpResponse, 'Error while purging cache: %s');
186 }
187 }
188
189 return false;
190 }
191
192 public function purgeByTag($tag, $reason = NULL) {
193 $this->isBacklogEnabled = true;
194 $path = 'cache/purge/' . $this->siteId;
195
196 $params = array();
197
198 $params["tag"] = $tag;
199
200 if ($reason) {
201 $params["reason"] = $reason;
202 }
203
204 try {
205 $httpResponse = $this->makeRequest($path, array(), array(), 'POST', $params);
206 } catch (\NitroPack\SocketReadTimedOutException $e) {
207 throw new \RuntimeException(sprintf('Timeout while purging cache by tag: %s', $tag));
208 }
209
210 $status = ResponseStatus::getStatus($httpResponse->getStatusCode());
211 switch ($status) {
212 case ResponseStatus::OK:
213 $body = json_decode($httpResponse->getBody(), true);
214
215 return isset($body['purged_urls']) ? $body['purged_urls'] : array();
216 default:
217 $this->throwException($httpResponse, 'Error while purging cache by tag: %s');
218 }
219
220 return array();
221 }
222
223 private function isCacheWarmupRequest() {
224 return isset($_SERVER['HTTP_X_NITRO_WARMUP']);
225 }
226 }
227