Base.php
5 years ago
Cache.php
5 years ago
Integration.php
6 years ago
RemoteConfigFetcher.php
6 years ago
RequestMaker.php
5 years ago
Response.php
6 years ago
ResponseStatus.php
6 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
223 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 | $httpResponse = $this->makeRequest($path, $headers, $cookies); |
| 39 | $status = ResponseStatus::getStatus($httpResponse->getStatusCode()); |
| 40 | $body = $httpResponse->getBody(); |
| 41 | $response = new Response($status, $body); |
| 42 | return $response; |
| 43 | } |
| 44 | |
| 45 | public function getLastPurge() { |
| 46 | $this->isBacklogEnabled = false; |
| 47 | $path = 'cache/getlastpurge/' . $this->siteId; |
| 48 | |
| 49 | $httpResponse = $this->makeRequest($path); |
| 50 | $status = ResponseStatus::getStatus($httpResponse->getStatusCode()); |
| 51 | switch ($status) { |
| 52 | case ResponseStatus::OK: |
| 53 | return json_decode($httpResponse->getBody(), true); |
| 54 | default: |
| 55 | $this->throwException($httpResponse, 'Error while getting information about the last cache purge: %s'); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public function purge($url = NULL, $pagecacheOnly = false, $reason = NULL) { |
| 60 | $this->isBacklogEnabled = true; |
| 61 | $path = 'cache/purge/' . $this->siteId; |
| 62 | |
| 63 | if (is_array($url)) { |
| 64 | $chunkSize = min(25, (int)ceil(count($url) * 0.2)); // 20% of the number of URLs up to 25 simultaneous requests |
| 65 | $requests = array(); |
| 66 | $cache = $this; |
| 67 | $retries = new \SplObjectStorage(); |
| 68 | $httpMulti = new \NitroPack\HttpClientMulti(); |
| 69 | |
| 70 | $httpMulti->onSuccess(function($client) use ($path, &$url, &$requests, $httpMulti, $cache, $reason) { |
| 71 | if ($client->getStatusCode() >= 500 && !empty($client->backlogEntry)) { |
| 72 | $this->addToBacklog($client->backlogEntry); |
| 73 | $this->nitropack && $this->nitropack->setHealthStatus(HealthStatus::UNDER_THE_WEATHER); |
| 74 | } |
| 75 | |
| 76 | if ($url) { |
| 77 | $_url = array_shift($url); |
| 78 | $params = array(); |
| 79 | $params["url"] = $_url; |
| 80 | |
| 81 | if ($reason) { |
| 82 | $params["reason"] = $reason; |
| 83 | } |
| 84 | |
| 85 | $httpClient = $cache->makeRequestAsync($path, array(), array(), 'POST', $params); |
| 86 | $httpMulti->push($httpClient); |
| 87 | $requests[] = $httpClient; |
| 88 | } |
| 89 | }); |
| 90 | |
| 91 | $httpMulti->onError(function($client, $exception) use ($httpMulti, $retries) { |
| 92 | if ($exception instanceof \NitroPack\SocketReadTimedOutException) { |
| 93 | if (!empty($client->backlogEntry)) { |
| 94 | $this->addToBacklog($client->backlogEntry); |
| 95 | $this->nitropack && $this->nitropack->setHealthStatus(HealthStatus::SICK); |
| 96 | } |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | if (!$retries->offsetExists($client)) { |
| 101 | $clientRetries = 0; |
| 102 | } else { |
| 103 | $clientRetries = $retries->offsetGet($client); |
| 104 | } |
| 105 | |
| 106 | if ($clientRetries < 5) { |
| 107 | $retries->offsetSet($client, $clientRetries + 1); |
| 108 | $client->replay(); |
| 109 | $httpMulti->push($client); |
| 110 | } else { |
| 111 | if (!empty($client->backlogEntry)) { |
| 112 | $this->addToBacklog($client->backlogEntry); |
| 113 | } |
| 114 | } |
| 115 | }); |
| 116 | |
| 117 | while ($url && count($httpMulti->getClients()) < $chunkSize) { |
| 118 | $_url = array_shift($url); |
| 119 | $params = array(); |
| 120 | $params["url"] = $_url; |
| 121 | |
| 122 | if ($reason) { |
| 123 | $params["reason"] = $reason; |
| 124 | } |
| 125 | |
| 126 | try { |
| 127 | $httpClient = $this->makeRequestAsync($path, array(), array(), 'POST', $params); |
| 128 | } catch (ServiceDownException $e) { |
| 129 | continue; |
| 130 | } |
| 131 | |
| 132 | $httpMulti->push($httpClient); |
| 133 | $requests[] = $httpClient; |
| 134 | } |
| 135 | |
| 136 | $res = $httpMulti->readAll(); // This blocks untill all requests have finished |
| 137 | |
| 138 | foreach ($res[1] as $failedRequest) { |
| 139 | $exception = $failedRequest[1]; |
| 140 | |
| 141 | if ($exception instanceof \NitroPack\SocketReadTimedOutException) { |
| 142 | continue; // Ignore read timeouts |
| 143 | } else { |
| 144 | throw $exception; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | foreach ($res[0] as $httpResponse) { |
| 149 | $status = ResponseStatus::getStatus($httpResponse->getStatusCode()); |
| 150 | if ($status !== ResponseStatus::OK) { |
| 151 | $this->throwException($httpResponse, 'Error while purging cache: %s'); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return true; |
| 156 | } else { |
| 157 | $params = array(); |
| 158 | if ($url) { |
| 159 | $params["url"] = $url; |
| 160 | } |
| 161 | |
| 162 | if ($pagecacheOnly) { |
| 163 | $params["pagecache_only"] = true; |
| 164 | } |
| 165 | |
| 166 | if ($reason) { |
| 167 | $params["reason"] = $reason; |
| 168 | } |
| 169 | |
| 170 | try { |
| 171 | $httpResponse = $this->makeRequest($path, array(), array(), 'POST', $params); |
| 172 | } catch (\NitroPack\SocketReadTimedOutException $e) { |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | $status = ResponseStatus::getStatus($httpResponse->getStatusCode()); |
| 177 | switch ($status) { |
| 178 | case ResponseStatus::OK: |
| 179 | return true; |
| 180 | default: |
| 181 | $this->throwException($httpResponse, 'Error while purging cache: %s'); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | public function purgeByTag($tag, $reason = NULL) { |
| 189 | $this->isBacklogEnabled = true; |
| 190 | $path = 'cache/purge/' . $this->siteId; |
| 191 | |
| 192 | $params = array(); |
| 193 | |
| 194 | $params["tag"] = $tag; |
| 195 | |
| 196 | if ($reason) { |
| 197 | $params["reason"] = $reason; |
| 198 | } |
| 199 | |
| 200 | try { |
| 201 | $httpResponse = $this->makeRequest($path, array(), array(), 'POST', $params); |
| 202 | } catch (\NitroPack\SocketReadTimedOutException $e) { |
| 203 | throw new \RuntimeException(sprintf('Timeout while purging cache by tag: %s', $tag)); |
| 204 | } |
| 205 | |
| 206 | $status = ResponseStatus::getStatus($httpResponse->getStatusCode()); |
| 207 | switch ($status) { |
| 208 | case ResponseStatus::OK: |
| 209 | $body = json_decode($httpResponse->getBody(), true); |
| 210 | |
| 211 | return isset($body['purged_urls']) ? $body['purged_urls'] : array(); |
| 212 | default: |
| 213 | $this->throwException($httpResponse, 'Error while purging cache by tag: %s'); |
| 214 | } |
| 215 | |
| 216 | return array(); |
| 217 | } |
| 218 | |
| 219 | private function isCacheWarmupRequest() { |
| 220 | return isset($_SERVER['HTTP_X_NITRO_WARMUP']); |
| 221 | } |
| 222 | } |
| 223 |