right the problem is a smallish one but hope someone can help.
Basically we use memcache to cache some of our mysql queries and then store the results in an array for memcache. Then when the query runs again it grabs the array results from memcache and processes the results like normal.
So the code looks like this.
$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 basically just either runs the query or returns the array from memcache.
The mysql_mem_fetch_array either processes the results from mysql or transverses the array.
The transversing part uses this code.
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;
}
The problem is the current and next commands – in large arrays (which are very rare) it causes some hanging.
Currently we are in php version 5.1.6 and we are going to 5.3 will the problem be solved?
Or is there a better way to handle the array?
Thanks for your help.
Richard
Easy. Just avoid storing large arrays in memcache.