フォーラムのトピック/スレッドがメンバーによって表示されるたびに、トピック テーブルが更新され、合計ビュー数が 1 つ増えます。
私は、すべてのビューで更新を行うのではなく、各トピックのビューを蓄積し、- (方法?) ビューを追加してから、cron を介して定期的に合計ビューの更新を行う方法についての回答を求めています - (方法? ) 更新をキューに入れる - 他のオプション?
フォーラムのトピック/スレッドがメンバーによって表示されるたびに、トピック テーブルが更新され、合計ビュー数が 1 つ増えます。
私は、すべてのビューで更新を行うのではなく、各トピックのビューを蓄積し、- (方法?) ビューを追加してから、cron を介して定期的に合計ビューの更新を行う方法についての回答を求めています - (方法? ) 更新をキューに入れる - 他のオプション?
Static variable
ortemp table
を使用してカウントを維持し、後で一定期間テーブルを更新することをお勧めします。
トピック ビューの数をキャッシュし、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";
}