Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 查看论坛主题时如何减少mysql更新_Php_Mysql_Delay_Forum_Accumulate - Fatal编程技术网

Php 查看论坛主题时如何减少mysql更新

Php 查看论坛主题时如何减少mysql更新,php,mysql,delay,forum,accumulate,Php,Mysql,Delay,Forum,Accumulate,每次成员查看论坛上的主题/线程时,都会对主题表进行更新,以将总浏览量增加1 我在寻找可能的方法,不更新每个视图,而是积累每个主题和主题的视图 -(如何?)添加视图,然后通过cron定期更新汇总视图 -(如何?)对更新进行排队 -其他选项?您可以尝试缓存主题视图的数量,并通过cron每隔X分钟运行一次更新查询,或者每N个主题视图检查一次以运行查询。 对于要查看正确数量的主题/论坛视图的用户,将返回缓存值。 使用APC /*a small example using Topic ID and inc

每次成员查看论坛上的主题/线程时,都会对主题表进行更新,以将总浏览量增加1

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

您可以尝试缓存主题视图的数量,并通过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";
}
/*一个使用主题ID和inc编号的小示例*/
$TopicID=123;
如果(apc_存在($TopicID)){
echo“TotalViews:.apc_inc($TopicID,1)。”
; }否则{ //查询数据库中的视图数量,并缓存该数量,比如10 apc_商店(TopicID,10美元); echo“TotalViews:.apc_inc($TopicID,1)。”
; } /**********/ /*一个更大的示例使用ForumIndex保存所有ID,用于运行cron作业和更新数据库*/ $ForumIndex=数组( (“线程”)=>数组( (456) => 1000 ), (“主题”)=>数组( (123)=>10 ) ); 如果(apc_存在(“论坛_索引”){//如果存在 $currentIndex=apc_fetch(“论坛索引”);//获取当前索引 $currentIndex[“主题”][]=数组(//添加新主题 (1234)=>124 ); $currentIndex[“threads”][456]+=1;//将ID为456的线程增加1个视图 apc_商店(“论坛索引”,$currentIndex);//recache var_dump(apc_fetch(“论坛索引”);//查看缓存数据 }否则{//它不存在 /*从数据库中获取视图的值*/ //构建$ForumIndex数组并缓存它 apc_商店(“论坛索引”,美元ForumIndex); 变量转储(apc获取(“论坛索引”); } /*每10分钟运行一次cron作业,以更新数据库中的内容*/ 如果(apc_存在(“论坛_索引”)){ $Index=apc_fetch(“论坛索引”); foreach($ID=>$totalViews的索引){ //执行更新查询 } //删除缓存数据或不执行任何操作并继续使用缓存 }否则{ echo“已结束cron作业..无事可做”; }
我建议使用
静态变量
临时表
来维护计数,然后在一段时间内更新该表。

您真的需要在主题表中存储视图计数吗?创建另一个-使用
topic,user,cnt
fields谢谢,我认为静态变量只能持续脚本的时间。由于有多个成员查看多个主题,因此不确定这将如何工作。不是吗?静态是每个类一个
。您可以将其存储到服务器重新启动,在此期间您可以将其保存到数据库并再次初始化。谢谢。在发布问题之前,我试图找到一些缓存示例,但找不到任何与问题相关的。可能是不正确的关键词!每个主题都需要缓存其视图,这增加了复杂性。我认为它没有那么复杂,存储一个带有主题/论坛ID、前缀或否(以避免冲突)的键,并为其分配视图值,此外,并非整个论坛/主题ID都将存储在缓存中,只有那些经常访问的主题/线程/论坛才会存储在缓存中,除非您的论坛/主题在10分钟内被成千上万的用户访问。。添加了一些使用APC的小例子,希望对您有所帮助谢谢GeoPhoenix,我会投票支持您,但我的低级代表不允许。我目前正在使用eaccelerator,所以可能是时候切换到APC了。