每次成员查看论坛上的主题/主题时,都会对主题表进行更新,以将总观看次数增加一个。
我正在回答可能的方法,不对每个视图进行更新,而是为每个主题积累视图 - (如何?)添加视图,然后通过cron定期更新总计视图 - (如何?)对更新进行排队 - 其他选择?
答案 0 :(得分:1)
我建议使用Static variable
或temp table
来维护计数,然后在一段时间内更新表格。
答案 1 :(得分:0)
您可以尝试缓存主题视图的数量,并通过cron每隔X分钟运行一次更新查询,或者检查每N个主题视图以运行查询。 用户可以看到正确数量的主题/论坛视图返回缓存值 使用APC
/*a small example using Topic ID and inc number*/
$TopicID=123;
if(apc_exists($TopicID)){
echo "TotalViews : ".apc_inc($TopicID,1)."<br/>";
}else{
// query database for numbers of views and cache that number, say its 10
apc_store($TopicID,10);
echo "TotalViews : ".apc_inc($TopicID,1)."<br/>";
}
/**********/
/*a larger example using a ForumIndex to hold all IDs, usefull for running a cron job and update Database*/
$ForumIndex = array(
("threads")=>array(
(456) => 1000
),
("topics")=>array(
(123)=>10
)
);
if(apc_exists("forum_index")){ // if it exists
$currentIndex = apc_fetch("forum_index"); // get current Index
$currentIndex["topics"][] = array( // add a new topic
(1234)=>124
);
$currentIndex["threads"][456] += 1; // Increase threads with ID 456 by 1 view
apc_store("forum_index",$currentIndex); // recache
var_dump(apc_fetch("forum_index")); // view cached data
}else{ // it doesn't exists
/*Fetch from database the value of the views */
// Build $ForumIndex array and cache it
apc_store("forum_index",$ForumIndex);
var_dump(apc_fetch("forum_index"));
}
/*a cron job to run every 10 min to update stuff at database*/
if(apc_exists("forum_index")){
$Index = apc_fetch("forum_index");
foreach($Index as $ID => $totalViews){
// execute update query
}
// delete cached data or do nothing and continue using cache
}else{
echo "Ended cron job .. nothing to do";
}