0

每次成员查看论坛上的主题/线程时,都会在主题表上进行更新以将总查看次数增加一。

我正在寻找不更新每个视图的可能方法的答案,而是为每个主题积累视图并 - (如何?)添加视图,然后通过 cron 定期更新汇总视图 - (如何? )排队更新 - 其他选项?

4

2 回答 2

1

我建议使用Static variabletemp table维护计数,然后在一段时间内更新表。

于 2012-07-05T11:59:35.403 回答
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";
}
于 2012-07-05T11:57:15.350 回答