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