protected function warmCache($key) { if (self::$_cacheWarmupInitiated) { return; } self::$_cacheWarmupInitiated = true; $key = "cache-warmup-{$key}"; $cacheSections = kCacheManager::getCacheSectionNames(kCacheManager::CACHE_TYPE_API_WARMUP); if (!$cacheSections) { return; } foreach ($cacheSections as $cacheSection) { $cacheStore = kCacheManager::getCache($cacheSection); if (!$cacheStore) { return; } // abort warming if a previous warmup started less than 10 seconds ago if ($cacheStore->get($key) !== false) { return; } // flag we are running a warmup for the current request $cacheStore->set($key, true, self::WARM_CACHE_TTL); } $uri = $_SERVER["REQUEST_URI"]; $fp = fsockopen(kConf::get('api_cache_warmup_host'), 80, $errno, $errstr, 1); if ($fp === false) { error_log("warmCache - Couldn't open a socket [" . $uri . "]", 0); return; } $method = $_SERVER["REQUEST_METHOD"]; $out = "{$method} {$uri} HTTP/1.1\r\n"; $sentHeaders = self::getRequestHeaders(); $sentHeaders["Connection"] = "Close"; // mark request as a warm cache request in order to disable caching and pass the http/https protocol (the warmup always uses http) $sentHeaders[self::WARM_CACHE_HEADER] = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? "https" : "http"; // if the request wasn't proxied pass the ip on the X-FORWARDED-FOR header $ipHeader = infraRequestUtils::getSignedIpAddressHeader(); if ($ipHeader) { list($headerName, $headerValue) = $ipHeader; $sentHeaders[$headerName] = $headerValue; } foreach ($sentHeaders as $header => $value) { $out .= "{$header}:{$value}\r\n"; } $out .= "\r\n"; if ($method == "POST") { $postParams = array(); foreach ($_POST as $key => &$val) { if (is_array($val)) { $val = implode(',', $val); } $postParams[] = $key . '=' . urlencode($val); } $out .= implode('&', $postParams); } fwrite($fp, $out); fclose($fp); }