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; }
$stmt = $db->prepare($sql); $stmt->bindParam(':account', $account, PDO::PARAM_STR); $stmt->bindParam(':password', $password, PDO::PARAM_STR); $stmt->execute(); if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $pid = $row['id']; $uid = $row['uid']; } if ($uid <= 0) { log_info("login fail, account:{$account} or passowrd:{$password} error"); $res = show_info('fail', '登录失败, 帐号或密码错误'); echo json_encode($res); return 0; } // 查询用户是否已经在线了 $tpid = mc_get($uid); if ($tpid != false && strlen($tpid) > 0) { // 发送登录通知给正在在线用户 // ..... log_info("login fail, uid:{$uid} has online"); $res = show_info('fail', '登录失败, 该帐号已经登录,请从另一设备上退出后再登录'); echo json_encode($res); return 0; } // 查询用户信息 $sql = "SELECT nickname, birthday, gender, status FROM sc_user WHERE id = :uid LIMIT 1"; $stmt = $db->prepare($sql); $stmt->bindParam(':uid', $uid, PDO::PARAM_INT); $stmt->execute(); if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $mydata['id'] = $uid;
function send_notice_to_uid($fuser_type, $fuid, $fnick, $tuid, $tpid, $tag_type, $qid) { $ret = false; // 检查收件人是否在线 $tpid = mc_get($tuid); if ($tpid != false && strlen($tpid) > 0) { // 用户在线,使用Socket给用户发送通知 log_info("send_notice_with_socket qid:{$qid}"); $ret = send_notice_with_socket($tpid, $qid, $fuser_type, $fuid, $fnick, $tuid, $tag_type); if ($ret == true) { return $ret; } } // 用户不在线,使用APNS发送通知 // 判断是否可以使用apns推送 if ($tpid > 0) { // 有风险,可能发件人被收件人设置隐藏 log_info("don't send APNS, maybe receiver set hidden to sender"); return 0; } $ret = send_notice_with_apns($qid, $fuser_type, $fuid, $fnick, $tuid, $tag_type); return $ret; }