Base.php
4 years ago
Cache.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
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
255 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 | public function enableCartCache() { |
| 224 | $path = 'cache/enablecartcache/' . $this->siteId; |
| 225 | |
| 226 | $httpResponse = $this->makeRequest($path, array(), array(), 'POST'); |
| 227 | |
| 228 | $status = ResponseStatus::getStatus($httpResponse->getStatusCode()); |
| 229 | switch ($status) { |
| 230 | case ResponseStatus::OK: |
| 231 | return true; |
| 232 | default: |
| 233 | $this->throwException($httpResponse, 'Error while enabling cart cache: %s'); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | public function disableCartCache() { |
| 238 | $path = 'cache/disablecartcache/' . $this->siteId; |
| 239 | |
| 240 | $httpResponse = $this->makeRequest($path, array(), array(), 'POST'); |
| 241 | |
| 242 | $status = ResponseStatus::getStatus($httpResponse->getStatusCode()); |
| 243 | switch ($status) { |
| 244 | case ResponseStatus::OK: |
| 245 | return true; |
| 246 | default: |
| 247 | $this->throwException($httpResponse, 'Error while disabling cart cache: %s'); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | private function isCacheWarmupRequest() { |
| 252 | return isset($_SERVER['HTTP_X_NITRO_WARMUP']); |
| 253 | } |
| 254 | } |
| 255 |