public function CachedQuery($query, $parameters = array(), $expiry = 60) { /* TODO: Do type guessing before checking cache, so as to avoid * different parameter hashes depending on input type for * numbers. */ $query_hash = md5($query); $parameter_hash = md5(serialize($parameters)); $cache_hash = $query_hash . $parameter_hash; $return_object = new stdClass(); if ($expiry != 0 && ($result = mc_get($cache_hash))) { $return_object->source = "memcache"; $return_object->data = $result; } else { $statement = $this->prepare($query); if (count($parameters) > 0) { foreach ($parameters as $key => $value) { $type = $this->GuessType($value); if (preg_match("/^[0-9]+\$/", $value) && is_string($value)) { /* PDO library apparently thinks it's part of a strongly typed language and doesn't do any typecasting. * We'll do it ourselves then. */ $int_value = (int) $value; if ($int_value < PHP_INT_MAX) { /* We only want to cast to integer if the result doesn't exceed INT_MAX, to avoid overflows. The * only way to do this appears to be aborting when it *equals* or exceeds INT_MAX, as an overflow * would occur during this check also. */ $value = $int_value; $type = PDO::PARAM_INT; } } if ($type == PDO::PARAM_STR) { $value = strval($value); } $statement->bindValue($key, $value, $type); } } if ($statement->execute() === true) { if ($result = $statement->fetchAll(PDO::FETCH_ASSOC)) { if (count($result) > 0) { if ($expiry != 0) { mc_set($cache_hash, $result, $expiry); } $return_object->source = "database"; $return_object->data = $result; } else { return false; } } else { /* There were zero results. Return null instead of an object without results, to allow for statements * of the form if($result = $database->CachedQuery()) . */ return null; } } else { /* The query failed. */ $err = $statement->errorInfo(); throw new DatabaseException("The query failed: {$err[2]}", 0, null, array('query' => $query, 'parameters' => $parameters)); } } return $return_object; }
function file_get_contents_cached($path, $expiry = 3600) { if ($res = mc_get(md5($path) . md5($path . "x"))) { $return_object->source = "memcache"; $return_object->data = $res; return $return_object; } else { if ($result = file_get_contents($path)) { $return_object->source = "disk"; $return_object->data = $result; mc_set(md5($path) . md5($path . "x"), $return_object->data, $expiry); return $return_object; } else { return false; } } }
function purify_html($input, $cache_duration = 3600, $config = null) { if (isset($config)) { $config->set("Cache.SerializerPath", "{$tmp_dir}/cphp/cache/htmlpurifier"); $cphp_purifier = new HTMLPurifier($config); $hash_config = md5(serialize($config)); } else { global $cphp_purifier; global $cphp_hash_purifier_config; $hash_config = $cphp_hash_purifier_config; } $hash_input = md5($input) . md5($input . "x"); $memcache_key = "purify_{$hash_config}_{$hash_input}"; if ($result = mc_get($memcache_key)) { return $result; } else { $result = $cphp_purifier->purify($input); mc_set($memcache_key, $result, $cache_duration); return $result; } }
public static function countRegexpMatches($regexp, $hasDiacritics, $sourceId, $useMemcache) { if ($useMemcache) { $key = "regexpCount_" . ($hasDiacritics ? '1' : '0') . "_" . ($sourceId ? $sourceId : 0) . "_{$regexp}"; $result = mc_get($key); if ($result) { return $result; } } $mysqlRegexp = StringUtil::dexRegexpToMysqlRegexp($regexp); $field = $hasDiacritics ? 'formNoAccent' : 'formUtf8General'; try { $result = $sourceId ? db_getSingleValue("select count(distinct L.id) from Lexem L join LexemDefinitionMap on L.id = lexemId join Definition D on definitionId = D.id " . "where {$field} {$mysqlRegexp} and sourceId = {$sourceId} order by formNoAccent") : Model::factory('Lexem')->where_raw("{$field} {$mysqlRegexp}")->count(); } catch (Exception $e) { $result = 0; // Bad regexp } if ($useMemcache) { mc_set($key, $result); } return $result; }