【发布时间】:2013-02-14 21:32:01
【问题描述】:
是的,问题很小,但希望有人能提供帮助。 基本上,我们使用 memcache 来缓存我们的一些 mysql 查询,然后将结果存储在 memcache 的数组中。然后,当查询再次运行时,它会从 memcache 中获取数组结果并像平常一样处理结果。 所以代码看起来像这样。
$query = "SELECT x,y,z FROM a WHERE b = 3";
$result = mysql_query_run($query,'memcache_name');
while($row = mysql_mem_fetch_array($result,'memcache_name')) {
//do processing
}
mysql_query_run 基本上只是运行查询或从内存缓存返回数组。 mysql_mem_fetch_array 要么处理来自 mysql 的结果,要么遍历数组。
横断部分使用此码。
if(is_array($result)) {
$return = current($result);
//get the current result - based on the internal pointer
next($result);//increment pointed
return $return;//return result
}
else {
//mysql result tab
//so get the array from the mysql_fetch_array
$array = mysql_fetch_array($result);
if(is_array($MEMCACHE_SERVER_RESULTS[$memcache])==false) {
//if there is no results then just set the results as array
$MEMCACHE_SERVER_RESULTS[$memcache] = array();
}
//push the array onto the end of the current array - from memcache
//if there are some results then push them on
if($single_row == 1) {
//only one row so just combine the 2 steps and store
array_push($MEMCACHE_SERVER_RESULTS[$memcache],$array);
$MEMCACHE_SERVER->set($memcache, $MEMCACHE_SERVER_RESULTS[$memcache],0,$memcache_time);
}
else if($array!==false) {
array_push($MEMCACHE_SERVER_RESULTS[$memcache],$array);
//and set it
}
else {
//set the memcache to the array that it has been making.
$MEMCACHE_SERVER->set($memcache, $MEMCACHE_SERVER_RESULTS[$memcache],0,$memcache_time);
}
//return array
return $array;
}
问题在于当前和下一个命令 - 在大型数组中(非常罕见),它会导致一些挂起。 目前我们在 php 版本 5.1.6 并且我们将要到 5.3 会解决问题吗? 还是有更好的方法来处理数组? 谢谢你的帮助。 理查德
【问题讨论】:
标签: php while-loop memcached each