/** * Get value from cache for the given key * @param string $key */ private static function getFromCache($key, $roleCacheDirtyAt) { if (!self::useCache()) { return null; } self::$cacheStores = array(); $cacheLayers = kCacheManager::getCacheSectionNames(kCacheManager::CACHE_TYPE_PERMISSION_MANAGER); foreach ($cacheLayers as $cacheLayer) { $cacheStore = kCacheManager::getCache($cacheLayer); if (!$cacheStore) { continue; } $cacheRole = $cacheStore->get(self::getCacheKeyPrefix() . $key); // try to fetch from cache if (!$cacheRole || !isset($cacheRole['updatedAt']) || $cacheRole['updatedAt'] < $roleCacheDirtyAt) { self::$cacheStores[] = $cacheStore; continue; } $map = $cacheStore->get(self::getCacheKeyPrefix() . $cacheRole['mapHash']); // try to fetch from cache if (!$map) { self::$cacheStores[] = $cacheStore; continue; } KalturaLog::debug("Found a cache value for key [{$key}] map hash [" . $cacheRole['mapHash'] . "] in layer [{$cacheLayer}]"); self::storeInCache($key, $cacheRole, $map); // store in lower cache layers self::$cacheStores[] = $cacheStore; return $map; } KalturaLog::debug("No cache value found for key [{$key}]"); return null; }
/** * @return array<kBaseCacheWrapper> */ protected static function getStores($cacheType = kCacheManager::CACHE_TYPE_RESPONSE_PROFILE) { if (isset(self::$cacheStores[$cacheType])) { return self::$cacheStores[$cacheType]; } self::$cacheStores[$cacheType] = array(); $cacheSections = kCacheManager::getCacheSectionNames($cacheType); if (is_array($cacheSections)) { foreach ($cacheSections as $cacheSection) { $cacheStore = kCacheManager::getCache($cacheSection); if ($cacheStore) { self::$cacheStores[$cacheType][] = $cacheStore; } } } return self::$cacheStores[$cacheType]; }
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); }
public static function getSecretsFromCache($partnerId) { $cacheSections = kCacheManager::getCacheSectionNames(kCacheManager::CACHE_TYPE_PARTNER_SECRETS); if (!$cacheSections) { return null; } $cacheKey = self::getSecretsCacheKey($partnerId); $lowerStores = array(); foreach ($cacheSections as $cacheSection) { $cacheStore = kCacheManager::getCache($cacheSection); if (!$cacheStore) { continue; } $secrets = $cacheStore->get($cacheKey); if (!$secrets) { $lowerStores[] = $cacheStore; continue; } foreach ($lowerStores as $cacheStore) { $cacheStore->set($cacheKey, $secrets); } return $secrets; } return null; }
protected function getKSVersionAndSecret($partnerId) { $result = parent::getKSVersionAndSecret($partnerId); if ($result) { return $result; } $partner = PartnerPeer::retrieveByPK($partnerId); if (!$partner) { return array(1, null); } // VERY big problem $ksVersion = $partner->getKSVersion(); $cacheKey = self::getSecretsCacheKey($partnerId); $cacheSections = kCacheManager::getCacheSectionNames(kCacheManager::CACHE_TYPE_PARTNER_SECRETS); foreach ($cacheSections as $cacheSection) { $cacheStore = kCacheManager::getCache($cacheSection); if (!$cacheStore) { continue; } $cacheStore->set($cacheKey, array($partner->getAdminSecret(), $partner->getSecret(), $ksVersion)); } return array($ksVersion, $partner->getAdminSecret()); }