What is the best way to count page views in PHP/MySQL?

后端 未结 8 756
[愿得一人]
[愿得一人] 2020-12-07 09:47

And by best I mean most efficient, right now placing this on my post.php file is the only thing I can think of:

$query = mysql_query(\" UPDATE posts SET view         


        
8条回答
  •  粉色の甜心
    2020-12-07 10:28

    If memcache is an option in your server environment, here's another cool way to sample, but also keep up with the precise number (unlike my other answer):

    function recordPostPageView($page_id) {
        $memcache = new Memcached(); // you could also pull this instance from somewhere else, if you want a bit more efficiency*
    
        $key = "Counter for Post {$page_id}";
    
        if(!$memcache->get($key)) {
            $memcache->set($key, 0);
        }
    
        $new_count = $memcache->increment($key);
    
        // you could uncomment the following if you still want to notify mysql of the value occasionally
        /*
        $notify_mysql_interval = 100;
        if($new_count % $notify_mysql_interval == 0) {
            $query = mysql_query("UPDATE posts SET views = {$new_count} WHERE id = '{$page_id}' ");
            // execute query, etc
        }
        */
    
        return $new_count;
    }
    
    • And don't mind purists crying foul about Singletons. Or you could pass it into this function, if you're more purist than pragmatist :)

提交回复
热议问题