/** * * @action getUploadedFileTokenByFileName * @param string $fileName * @return KalturaUploadResponse */ function getUploadedFileTokenByFileNameAction($fileName) { KalturaResponseCacher::disableConditionalCache(); $res = new KalturaUploadResponse(); $ksUnique = md5($this->getKs()->toSecureString()); $uniqueId = md5($fileName); $ext = pathinfo($fileName, PATHINFO_EXTENSION); $token = $ksUnique . "_" . $uniqueId . "." . $ext; $entryFullPath = myUploadUtils::getUploadPath($token, "", null, strtolower($ext)); // filesync ok if (!file_exists($entryFullPath)) { throw new KalturaAPIException(KalturaErrors::UPLOADED_FILE_NOT_FOUND_BY_TOKEN); } $res->uploadTokenId = $token; $res->fileSize = kFile::fileSize($entryFullPath); return $res; }
public function execute($input_parameters = null) { if (class_exists('KalturaResponseCacher')) { KalturaResponseCacher::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) { 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"); } KalturaLog::debug("Sql took - " . (microtime(true) - $sqlStart) . " seconds"); } }
public function query() { if (class_exists('KalturaResponseCacher')) { KalturaResponseCacher::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"); } KalturaLog::debug("Sql took - " . (microtime(true) - $sqlStart) . " seconds"); return $result; }
public function get($obj_name) { if (!self::$s_ready) { return NULL; } // $this->m_stats->m_gets++; if (class_exists('KalturaResponseCacher')) { KalturaResponseCacher::disableConditionalCache(); } $value = self::$s_memcache->get($this->m_namespace . $obj_name); if (!isset($value)) { $this->m_stats->m_misses++; return NULL; } else { $this->m_stats->m_hits++; return $value; } }
public function adjustApiCacheForException($ex) { KalturaResponseCacher::setExpiry(120); $cacheConditionally = false; if ($ex instanceof KalturaAPIException && kConf::hasParam("v3cache_conditional_cached_errors")) { $cacheConditionally = in_array($ex->getCode(), kConf::get("v3cache_conditional_cached_errors")); } if (!$cacheConditionally) { KalturaResponseCacher::disableConditionalCache(); } }
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; $queryStart = microtime(true); $cacheResult = self::$s_memcacheKeys->multiGet($keysToGet); KalturaLog::debug("kQueryCache: keys query took " . (microtime(true) - $queryStart) . " seconds"); if ($cacheResult === false) { KalturaLog::debug("kQueryCache: failed to query keys memcache, not using query cache"); return null; } // don't cache the result if the 'dont cache' flag is enabled $cacheQuery = true; if (array_key_exists(self::DONT_CACHE_KEY, $cacheResult)) { if ($cacheResult[self::DONT_CACHE_KEY]) { KalturaLog::debug("kQueryCache: dontCache key is set -> not caching the result"); if (class_exists('KalturaResponseCacher')) { KalturaResponseCacher::disableConditionalCache(); } $cacheQuery = false; } unset($cacheResult[self::DONT_CACHE_KEY]); } // check whether we should query the master $queryDB = self::QUERY_DB_SLAVE; $currentTime = time(); foreach ($cacheResult as $invalidationKey => $invalidationTime) { if ($currentTime < $invalidationTime + self::QUERY_MASTER_TIME_MARGIN_SEC) { KalturaLog::debug("kQueryCache: changed recently -> query master, peer={$peerClassName}, invkey={$invalidationKey} querytime={$currentTime} invtime={$invalidationTime}"); $queryDB = self::QUERY_DB_MASTER; if ($currentTime < $invalidationTime + self::INVALIDATION_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 (class_exists('KalturaResponseCacher')) { $invalidationTime = 0; if ($cacheResult) { $invalidationTime = max($cacheResult); } KalturaResponseCacher::addInvalidationKeys($invalidationKeys, $invalidationTime); } // check whether we have a valid cached query $origCacheKey = self::CACHE_PREFIX_QUERY . $queryType . md5(serialize($criteria) . self::CACHE_VERSION); if ($cacheQuery) { $cacheKey = $origCacheKey; } $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; $existingInvKeys = array(); foreach ($cacheResult as $invalidationKey => $invalidationTime) { $existingInvKeys[] = "{$invalidationKey}:{$invalidationTime}"; if ($queryTime < $invalidationTime + self::INVALIDATION_TIME_MARGIN_SEC) { KalturaLog::debug("kQueryCache: cached query invalid, peer={$peerClassName}, key={$origCacheKey}, invkey={$invalidationKey} querytime={$queryTime} debugInfo={$debugInfo} invtime={$invalidationTime}"); return null; } } // return from memcache $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) { if (class_exists('KalturaResponseCacher')) { KalturaResponseCacher::disableConditionalCache(); } $mysql_function = strpos($query, 'CALL') === false ? 'mysql' : 'mysqli'; $db_config = kConf::get("reports_db_config"); $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); 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; }