public static function file_get_contents(FileSyncKey $key, $fetch_from_remote_if_no_local = true, $strict = true)
 {
     $cacheStore = kCacheManager::getCache(kCacheManager::MC_GLOBAL_FILESYNC);
     if ($cacheStore) {
         $cacheKey = self::CACHE_KEY_PREFIX . "{$key->object_id}_{$key->object_type}_{$key->object_sub_type}_{$key->version}";
         $result = $cacheStore->get($cacheKey);
         if ($result) {
             KalturaLog::log("returning from cache, key [{$cacheKey}] size [" . strlen($result) . "]");
             return $result;
         }
     }
     KalturaLog::log(__METHOD__ . " - key [{$key}], fetch_from_remote_if_no_local [{$fetch_from_remote_if_no_local}], strict [{$strict}]");
     list($file_sync, $local) = self::getReadyFileSyncForKey($key, $fetch_from_remote_if_no_local, $strict);
     if ($file_sync) {
         $file_sync = self::resolve($file_sync);
     }
     if ($file_sync) {
         $result = self::getContentsByFileSync($file_sync, $local, $fetch_from_remote_if_no_local, $strict);
         if ($cacheStore && $result && strlen($result) < self::MAX_CACHED_FILE_SIZE && !in_array($key->object_type, self::$uncachedObjectTypes)) {
             KalturaLog::log("saving to cache, key [{$cacheKey}] size [" . strlen($result) . "]");
             $cacheStore->set($cacheKey, $result, self::FILE_SYNC_CACHE_EXPIRY);
         }
         return $result;
     }
     KalturaLog::log(__METHOD__ . " - FileSync not found");
     return null;
 }
 /**
  * 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();
     foreach (self::$cacheLayers as $cacheLayer) {
         $cacheStore = kCacheManager::getCache($cacheLayer);
         if (!$cacheStore) {
             continue;
         }
         $value = $cacheStore->get(self::getCacheKeyPrefix() . $key);
         // try to fetch from cache
         if (!$value || !isset($value['updatedAt']) || $value['updatedAt'] < $roleCacheDirtyAt) {
             self::$cacheStores[] = $cacheStore;
             continue;
         }
         KalturaLog::debug("Found a cache value for key [{$key}] in layer [{$cacheLayer}]");
         self::storeInCache($key, $value);
         // store in lower cache layers
         self::$cacheStores[] = $cacheStore;
         // cache is updated - init from cache
         unset($value['updatedAt']);
         return $value;
     }
     KalturaLog::debug("No cache value found for key [{$key}]");
     return null;
 }
 protected static function initGlobalMemcache()
 {
     if (self::$s_memcacheInited) {
         return;
     }
     self::$s_memcacheInited = true;
     self::$s_memcacheKeys = kCacheManager::getCache(kCacheManager::MC_GLOBAL_KEYS);
     if (self::$s_memcacheKeys === null) {
         // no reason to init the queries server, the query cache won't be used anyway
         return;
     }
     self::$s_memcacheQueries = kCacheManager::getCache(kCacheManager::MC_GLOBAL_QUERIES);
 }
 /**
  * @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];
 }
 private function calculateCacheKey()
 {
     $this->getKsData();
     $this->_playbackContext = isset($this->_params['playbackContext']) ? $this->_params['playbackContext'] : null;
     unset($this->_params['playbackContext']);
     $this->_params['___cache___protocol'] = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? "https" : "http";
     $this->_params['___cache___host'] = @$_SERVER['HTTP_HOST'];
     $this->_params['___cache___version'] = self::CACHE_VERSION;
     // take only the hostname part of the referrer parameter of baseEntry.getContextData
     if (isset($this->_params['referrer'])) {
         $referrer = base64_decode(str_replace(" ", "+", $this->_params['referrer']));
         if (!is_string($referrer)) {
             $referrer = "";
         }
         unset($this->_params['referrer']);
     } else {
         $referrer = self::getHttpReferrer();
     }
     $this->_referrers[] = $referrer;
     $this->finalizeCacheKey();
     $this->addExtraFields();
     $this->_cacheWrapper = kCacheManager::getCache(kCacheManager::FS_PLAY_MANIFEST);
 }
Beispiel #6
0
 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);
 }
 protected function storeExtraFields()
 {
     if (!$this->_cacheKeyDirty) {
         return;
     }
     // no extra fields were added to the cache
     $extraFieldsCache = kCacheManager::getCache(kCacheManager::APC);
     if (!$extraFieldsCache) {
         self::disableCache();
         return;
     }
     if ($extraFieldsCache->set(self::EXTRA_KEYS_PREFIX . $this->_originalCacheKey, $this->_extraFields, self::CONDITIONAL_CACHE_EXPIRY) === false) {
         self::disableCache();
         return;
     }
     $this->finalizeCacheKey();
     // update the cache key to include the extra fields
 }
 /**
  * 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;
 }
Beispiel #9
0
 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;
 }
Beispiel #10
0
 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());
 }
 /**
  * @return KalturaPDO
  */
 public static function getSphinxConnection($read = true)
 {
     if (self::$sphinxConnection) {
         return self::$sphinxConnection;
     }
     $sphinxDS = isset(self::$config['sphinx_datasources']['datasources']) ? self::$config['sphinx_datasources']['datasources'] : array(self::DB_CONFIG_SPHINX);
     $cacheExpiry = isset(self::$config['sphinx_datasources']['cache_expiry']) ? self::$config['sphinx_datasources']['cache_expiry'] : 300;
     $connectTimeout = isset(self::$config['sphinx_datasources']['connect_timeout']) ? self::$config['sphinx_datasources']['connect_timeout'] : 1;
     $stickySessionExpiry = isset(self::$config['sphinx_datasources']['sticky_session_timeout']) ? self::$config['sphinx_datasources']['sticky_session_timeout'] : 600;
     $stickySessionKey = 'StickySession:' . kCurrentContext::$user_ip;
     $cache = kCacheManager::getCache(kCacheManager::MC_GLOBAL_QUERIES);
     if ($cache) {
         $key = $cache->get($stickySessionKey);
         if ($key) {
             $connection = self::getConnection($key, $cacheExpiry, $connectTimeout);
             if (!is_null($connection)) {
                 return $connection;
             }
         }
     }
     // loop twice, on first iteration try only connections not marked as failed
     // in case all connections failed, try all connections on second iteration
     $iteration = 2;
     while ($iteration--) {
         $count = count($sphinxDS);
         $offset = mt_rand(0, $count - 1);
         while ($count--) {
             $key = $sphinxDS[($count + $offset) % count($sphinxDS)];
             $cacheKey = "sphinxCon:" . $key;
             if (function_exists('apc_fetch')) {
                 if (!$iteration) {
                     // on second iteration reset failed connection flag
                     apc_store($cacheKey, 0);
                 } else {
                     if (apc_fetch($cacheKey)) {
                         // if connection failed to connect in the past mark it
                         continue;
                     }
                 }
             }
             $connection = self::getConnection($key, $cacheExpiry, $connectTimeout);
             if (!$read && $cache) {
                 $cache->set($stickySessionKey, $key, $stickySessionExpiry);
             }
             if (!is_null($connection)) {
                 return $connection;
             }
         }
     }
     KalturaLog::debug("getSphinxConnection: Failed to connect to any Sphinx config");
     throw new Exception('Failed to connect to any Sphinx config');
 }
 private static function getMaxInvalidationTime($invalidationKeys)
 {
     $memcache = kCacheManager::getCache(kCacheManager::MC_GLOBAL_KEYS);
     if (!$memcache) {
         return null;
     }
     $cacheResult = $memcache->multiGet($invalidationKeys);
     if ($cacheResult === false) {
         return null;
     }
     // failed to get the invalidation keys
     if (!$cacheResult) {
         return 0;
     }
     // no invalidation keys - no changes occured
     return max($cacheResult);
 }
 protected function isKSInvalidated()
 {
     if (strpos($this->privileges, self::PRIVILEGE_ACTIONS_LIMIT) !== false) {
         return null;
     }
     // cannot validate action limited KS at this level
     $memcache = kCacheManager::getCache(kCacheManager::MC_GLOBAL_KEYS);
     if (!$memcache) {
         return null;
     }
     // failed to connect to memcache or memcache not enabled
     $ksKey = self::INVALID_SESSION_KEY_PREFIX . $this->hash;
     $keysToGet = array(self::INVALID_SESSIONS_SYNCED_KEY, $ksKey);
     $cacheResult = $memcache->multiGet($keysToGet);
     if ($cacheResult === false) {
         return null;
     }
     // failed to get the keys
     if (!array_key_exists(self::INVALID_SESSIONS_SYNCED_KEY, $cacheResult) || !$cacheResult[self::INVALID_SESSIONS_SYNCED_KEY]) {
         return null;
     }
     // invalid sessions not synched to memcache
     if (array_key_exists($ksKey, $cacheResult)) {
         return true;
     }
     // the session is invalid
     return false;
 }