All pastes #91561 Raw Edit

Miscellany

public text v1 · immutable
#91561 ·published 2006-07-18 18:41 UTC
rendered paste body
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;
	}
}