public function apply() { if ($this->_cacheStatus == self::CACHE_STATUS_DISABLED) { kApiCache::disableCache(); return; } // common cache fields foreach ($this->_extraFields as $extraField) { call_user_func_array(array('kApiCache', 'addExtraField'), $extraField); } // anonymous cache fields if ($this->_expiry) { kApiCache::setExpiry($this->_expiry); } if ($this->_cacheStatus == self::CACHE_STATUS_ANONYMOUS_ONLY) { kApiCache::disableConditionalCache(); return; } // conditional cache fields if ($this->_conditionalCacheExpiry) { kApiCache::setConditionalCacheExpiry($this->_conditionalCacheExpiry); } kApiCache::addInvalidationKeys(array_keys($this->_invalidationKeys), $this->_invalidationTime); kApiCache::addSqlQueryConditions($this->_sqlConditions); }
public function get($obj_name) { if (!$this->m_cache) { return NULL; } kApiCache::disableConditionalCache(); $value = $this->m_cache->get($this->m_namespace . $obj_name); if (!isset($value)) { return NULL; } return $value; }
protected function doCall($operation, array $params = array(), $type = null) { kApiCache::disableConditionalCache(); $namespace = 'http://tempuri.org'; $soapAction = ''; $headers = array(); $headers["KALTURA_SESSION_ID"] = (string) new UniqueId(); $result = $this->call($operation, $params, $namespace, $soapAction, $headers); $this->throwError($result); if ($type) { return new $type($result); } return $result; }
public function execute($input_parameters = null) { if (!kQueryCache::isCurrentQueryHandled()) { kApiCache::disableConditionalCache(); } $search = array(); $replace = array(); if (is_null($input_parameters)) { $search = array_reverse(array_keys($this->values)); $replace = array_reverse($this->values); } else { $i = 1; foreach ($input_parameters as $value) { $search[] = ':p' . $i++; if (is_null($value)) { $replace[] = "NULL"; } else { $replace[] = "'{$value}'"; } } $search = array_reverse($search); $replace = array_reverse($replace); } $sql = str_replace($search, $replace, $this->queryString); KalturaLog::debug($sql); $sqlStart = microtime(true); if (self::$dryRun && !preg_match('/^(\\/\\*.+\\*\\/ )?SELECT/i', $sql)) { KalturaLog::debug("Sql dry run - " . (microtime(true) - $sqlStart) . " seconds"); } else { try { parent::execute($input_parameters); } catch (PropelException $pex) { KalturaLog::alert($pex->getMessage()); throw new PropelException("Database error"); } $sqlTook = microtime(true) - $sqlStart; KalturaLog::debug("Sql took - " . $sqlTook . " seconds"); KalturaMonitorClient::monitorDatabaseAccess($sql, $sqlTook); } }
/** * batch extendFileSyncLock action extends the expiration of a file sync lock * * @action extendFileSyncLock * @param int $id The id of the file sync */ function extendFileSyncLockAction($id) { // need to explicitly disable the cache since this action does not perform any queries kApiCache::disableConditionalCache(); $lockCache = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_LOCK_KEYS); if (!$lockCache) { throw new KalturaAPIException(MultiCentersErrors::GET_LOCK_CACHE_FAILED); } if (!$lockCache->set(self::LOCK_KEY_PREFIX . $id, true, self::LOCK_EXPIRY)) { throw new KalturaAPIException(MultiCentersErrors::EXTEND_FILESYNC_LOCK_FAILED); } }
public function query() { kApiCache::disableConditionalCache(); $args = func_get_args(); $sql = $args[0]; KalturaLog::debug($sql); $comment = $this->getCommentWrapped(); $sql = $comment . $sql; $sqlStart = microtime(true); try { if (version_compare(PHP_VERSION, '5.3', '<')) { $result = call_user_func_array(array($this, 'parent::query'), $args); } else { $result = call_user_func_array('parent::query', $args); } } catch (PropelException $pex) { KalturaLog::alert($pex->getMessage()); throw new PropelException("Database error"); } $sqlTook = microtime(true) - $sqlStart; KalturaLog::debug("Sql took - " . $sqlTook . " seconds"); KalturaMonitorClient::monitorDatabaseAccess($sql, $sqlTook, $this->hostName); return $result; }
public static function getCachedQueryResults(Criteria $criteria, $queryType, $peerClassName, &$cacheKey, &$queryDB) { if (!kConf::get("query_cache_enabled")) { return null; } // if the criteria has an empty IN, no need to go to the DB or memcache - return an empty array foreach ($criteria->getMap() as $criterion) { if (in_array(Criterion::ODER, $criterion->getConjunctions())) { continue; } if ($criterion->getComparison() == Criteria::IN && !$criterion->getValue()) { KalturaLog::debug("kQueryCache: criteria has empty IN, returning empty result set, peer={$peerClassName}"); return array(); } } // initialize $invalidationKeyRules = call_user_func(array($peerClassName, 'getCacheInvalidationKeys')); $invalidationKeys = self::getInvalidationKeysForQuery($invalidationKeyRules, $criteria); if (!$invalidationKeys) { return null; } self::initGlobalMemcache(); if (self::$s_memcacheQueries === null) { return null; } // build memcache query foreach ($invalidationKeys as $index => $invalidationKey) { $invalidationKeys[$index] = self::CACHE_PREFIX_INVALIDATION_KEY . $invalidationKey; } $keysToGet = $invalidationKeys; $keysToGet[] = self::DONT_CACHE_KEY; $keysToGet[] = self::MAX_SLAVE_LAG_KEY; $queryStart = microtime(true); $cacheResult = self::$s_memcacheKeys->multiGet($keysToGet); KalturaLog::debug("kQueryCache: keys query took " . (microtime(true) - $queryStart) . " seconds"); if ($cacheResult === false) { KalturaLog::log("kQueryCache: failed to query keys memcache, not using query cache"); return null; } // get max slave lag $queryMasterThreshold = self::MAX_QUERY_MASTER_TIME_MARGIN_SEC; $maxSlaveLag = null; if (array_key_exists(self::MAX_SLAVE_LAG_KEY, $cacheResult) && strlen($cacheResult[self::MAX_SLAVE_LAG_KEY]) && is_numeric($cacheResult[self::MAX_SLAVE_LAG_KEY])) { $maxSlaveLag = $cacheResult[self::MAX_SLAVE_LAG_KEY]; $maxSlaveLag += self::SLAVE_LAG_TIME_MARGIN_SEC; $queryMasterThreshold = min($maxSlaveLag, $queryMasterThreshold); } unset($cacheResult[self::MAX_SLAVE_LAG_KEY]); // don't cache the result if the 'dont cache' flag is enabled $cacheQuery = true; if (array_key_exists(self::DONT_CACHE_KEY, $cacheResult) && $cacheResult[self::DONT_CACHE_KEY]) { KalturaLog::log("kQueryCache: dontCache key is set -> not caching the result"); $cacheQuery = false; } unset($cacheResult[self::DONT_CACHE_KEY]); // get max invalidation time $maxInvalidationTime = null; $maxInvalidationKey = null; if (count($cacheResult)) { $maxInvalidationTime = max($cacheResult); $maxInvalidationKey = array_search($maxInvalidationTime, $cacheResult); } // check whether we should query the master $queryDB = self::QUERY_DB_SLAVE; $currentTime = time(); if (!is_null($maxInvalidationTime) && $currentTime < $maxInvalidationTime + $queryMasterThreshold) { KalturaLog::debug("kQueryCache: changed recently -> query master, peer={$peerClassName}, invkey={$maxInvalidationKey} querytime={$currentTime} invtime={$maxInvalidationTime} threshold={$queryMasterThreshold}"); $queryDB = self::QUERY_DB_MASTER; if ($currentTime < $maxInvalidationTime + self::CLOCK_SYNC_TIME_MARGIN_SEC) { return null; // The query won't be cached since cacheKey is null, it's ok cause it won't be used anyway } } if ($queryDB == self::QUERY_DB_SLAVE && !is_null($maxInvalidationTime) && $currentTime < $maxInvalidationTime + $maxSlaveLag) { KalturaLog::debug("kQueryCache: using an out of date slave -> not caching the result, peer={$peerClassName}, invkey={$maxInvalidationKey} querytime={$currentTime} invtime={$maxInvalidationTime} slavelag={$maxSlaveLag}"); $cacheQuery = false; } // get the cache key and update the api cache $origCacheKey = self::CACHE_PREFIX_QUERY . $queryType . md5(serialize($criteria) . self::CACHE_VERSION); if ($cacheQuery) { kApiCache::addInvalidationKeys($invalidationKeys, $maxInvalidationTime); $cacheKey = new kQueryCacheKey($origCacheKey); } else { kApiCache::disableConditionalCache(); } // check whether we have a valid cached query $queryStart = microtime(true); $queryResult = self::$s_memcacheQueries->get($origCacheKey); KalturaLog::debug("kQueryCache: query took " . (microtime(true) - $queryStart) . " seconds"); if (!$queryResult) { KalturaLog::debug("kQueryCache: cache miss, peer={$peerClassName}, key={$origCacheKey}"); return null; } list($queryResult, $queryTime, $debugInfo) = $queryResult; if (!is_null($maxInvalidationTime) && $queryTime < $maxInvalidationTime + self::CLOCK_SYNC_TIME_MARGIN_SEC) { KalturaLog::debug("kQueryCache: cached query invalid, peer={$peerClassName}, key={$origCacheKey}, invkey={$maxInvalidationKey} querytime={$queryTime} debugInfo={$debugInfo} invtime={$maxInvalidationTime}"); return null; } // return from memcache $existingInvKeys = array(); foreach ($cacheResult as $invalidationKey => $invalidationTime) { $existingInvKeys[] = "{$invalidationKey}:{$invalidationTime}"; } $existingInvKeys = implode(',', $existingInvKeys); KalturaLog::debug("kQueryCache: returning from memcache, peer={$peerClassName}, key={$origCacheKey} queryTime={$queryTime} debugInfo={$debugInfo} invkeys=[{$existingInvKeys}]"); return $queryResult; }
private static function executeQuery($query) { kApiCache::disableConditionalCache(); $mysql_function = 'mysqli'; $db_config = kConf::get("reports_db_config"); if (!isset($db_config["port"])) { if (ini_get("mysqli.default_port") !== null) { $db_config["port"] = ini_get("mysqli.default_port"); } else { $db_config["port"] = 3306; } } $timeout = isset($db_config["timeout"]) ? $db_config["timeout"] : 40; ini_set('mysql.connect_timeout', $timeout); $host = $db_config["host"]; if (isset($db_config["port"]) && $db_config["port"] && $mysql_function != 'mysqli') { $host .= ":" . $db_config["port"]; } $connect_function = $mysql_function . '_connect'; $link = $connect_function($host, $db_config["user"], $db_config["password"], null, $db_config["port"]); KalturaLog::log("Reports query using database host: [{$host}] user [" . $db_config["user"] . "]"); if ($mysql_function == 'mysql') { $db_selected = mysql_select_db($db_config["db_name"], $link); } else { $db_selected = mysqli_select_db($link, $db_config["db_name"]); } $error_function = $mysql_function . '_error'; if (!$db_selected) { throw new kCoreException('Can\'t use foo : ' . $error_function($link), kCoreException::INVALID_QUERY); } if ($mysql_function == 'mysql') { $result = mysql_query($query); } else { $result = mysqli_query($link, $query); } // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { KalturaLog::err('Invalid query: ' . $error_function($link)); $message = 'Invalid query'; throw new kCoreException($message, kCoreException::INVALID_QUERY); } $res = array(); $fetch_function = $mysql_function . '_fetch_assoc'; while ($row = $fetch_function($result)) { $res[] = $row; } $free_result_func = $mysql_function . '_free_result'; $free_result_func($result); $close_function = $mysql_function . '_close'; $close_function($link); return $res; }
private static function executeQuery($query, $link) { kApiCache::disableConditionalCache(); $mysql_function = 'mysqli'; $db_config = kConf::get("reports_db_config"); if ($mysql_function == 'mysql') { $db_selected = mysql_select_db($db_config["db_name"], $link); } else { $db_selected = mysqli_select_db($link, $db_config["db_name"]); } $error_function = $mysql_function . '_error'; if (!$db_selected) { throw new kCoreException('Can\'t use foo : ' . $error_function($link), kCoreException::INVALID_QUERY); } if ($mysql_function == 'mysql') { $result = mysql_query($query); } else { $result = mysqli_query($link, $query); } // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { KalturaLog::err('Invalid query: ' . $error_function($link)); $message = 'Invalid query'; throw new kCoreException($message, kCoreException::INVALID_QUERY); } $res = array(); $fetch_function = $mysql_function . '_fetch_assoc'; while ($row = $fetch_function($result)) { $res[] = $row; } $free_result_func = $mysql_function . '_free_result'; $free_result_func($result); $close_function = $mysql_function . '_close'; $close_function($link); return $res; }
/** * batch checkFileExists action check if the file exists * * @action checkFileExists * @param string $localPath * @param float $size * @return KalturaFileExistsResponse */ function checkFileExistsAction($localPath, $size) { // need to explicitly disable the cache since this action does not perform any queries kApiCache::disableConditionalCache(); $ret = new KalturaFileExistsResponse(); $ret->exists = file_exists($localPath); $ret->sizeOk = false; if ($ret->exists) { clearstatcache(); $ret->sizeOk = kFile::fileSize($localPath) == $size; } return $ret; }