2024-10-10

This commit is contained in:
2024-10-10 12:56:56 +08:00
commit 7b347c848a
2648 changed files with 643965 additions and 0 deletions

59
web/include/memcache.php Normal file
View File

@@ -0,0 +1,59 @@
<?php
require_once(dirname(__FILE__)."/db_info.inc.php");
# Connect to memcache:
global $memcache;
if ($OJ_MEMCACHE){
$memcache = new Memcache;
if($OJ_SAE){
$memcache=memcache_init();
}else{
$memcache->connect($OJ_MEMSERVER, $OJ_MEMPORT);
}
}
//下面两个函数首先都会判断是否有使用memcache如果有使用就会调用memcached的set/get命令来保存和获取数据
//否则简单地返回false
# Gets key / value pair into memcache … called by mysql_query_cache()
function getCache($key) {
global $memcache;
// if ($memcache->get($key)) echo "true";
return ($memcache) ? $memcache->get($key) : false;
}
# Puts key / value pair into memcache … called by mysql_query_cache()
function setCache($key, $object, $timeout = 60) {
global $memcache;
return ($memcache) ? $memcache->set($key,$object,MEMCACHE_COMPRESSED,$timeout) : false;
}
# Caching version of pdo_query()
function mysql_query_cache($sql){
global $OJ_NAME,$OJ_MEMCACHE;
$linkIdentifier = false;
$timeout = 60;
//首先调用上面的getCache函数如果返回值不为false的话就说明是从memcached服务器获取的数据
//如果返回false此时就需要直接从数据库中获取数据了。
//需要注意的是这里使用操作的命令加上sql语句的md5码作为一个特定的key可能大家觉得使用数据项的
//名称作为key会比较自然一点。运行memcached加上"-vv"参数并且不作为daemon运行的话可以看见
//memcached处理时输出的相关信息
$num_args = func_num_args();
$args = func_get_args(); //获得传入的所有参数的数组
$args = array_slice($args,1,--$num_args);
$key=md5($OJ_NAME.$_SERVER['HTTP_HOST']."mysql_query" . $sql.implode(" ",$args));
if (!($cache = getCache($key))) {
$cache = false;
$cache =pdo_query($sql,$args);
//将数据放入memcached服务器中如果memcached服务器没有开的话此语句什么也不会做
//如果开启了服务器的话数据将会被缓存到memcached服务器中
if (!setCache($key, $cache, $timeout)) {
# If we get here, there isnt a memcache daemon running or responding
if($OJ_MEMCACHE) echo "You can run these command to get faster speed:<br>sudo apt-get install memcached<br>sudo apt-get install php5-memcache<br>sudo apt-get install php-memcache";
}
}
return $cache;
}
?>