function get_views($topics) { global $db, $mcache; // need an array of keys for memcache // loop is easier than a callback function foreach($topics as $id) { $input[] = "topic_views[".$id."]"; } // try and set an array from memcache foreach($mcache->get($input) as $key => $val) { eval("\$".$key." = ".$val.";"); } if(is_array($topic_views)) { $views = $topic_views; // create an array of missing keys $missing = array_diff($topics, array_keys($topic_views)); } else { $missing = $topics; } if(count($missing) > 0) { $compacted = implode("','", $missing); $result = $db->query("select topic, count(*) from topicstats where topic in ('".$compacted."') group by topic"); while($row = $result->fetch($result)) { $views[$row['topic']] = $row['count(*)']; $mcache->set("topic_views[".$row['topic']."]", $row['count(*)'], 60); } $result = NULL; } if(is_array($views)) { return $views; } else { return FALSE; } }