减少Wordpress数据库查询

时间:2013-10-25 18:44:58

标签: php sql wordpress

我的wordpress网站使用平铺格式,并在主页上显示10个帖子;一旦你开始滚动它会加载更多的无限滚动。

我正在尝试减少页面加载时间,因为添加此代码大约需要2.5秒;没有它,1秒钟。

我想要做的是抓住每个帖子每小时的社交份额,并将其显示在主页上。我试图找出一种快速/简单的方法,但这似乎是我所知道的唯一方法。

由于它显示了10个帖子,因此每个帖子必须列出2个请求,因此共有20个请求进入数据库。这似乎对我来说很重要,似乎有一些简单的方法可以做到这一点,我错过了。

任何帮助?

function getFBShares($postid, $url){

//Grab Results from fb_shares
    $fbcount  = get_post_meta( $postid, 'fb_shares', true );

    if( ! empty( $fbcount ) ) {
        $fbtime  = get_post_meta( $postid, 'share_time', true );
        //Compare timestamp with one hour ago
        if ($fbtime < current_time('timestamp') - 60*60){
            $fql = "SELECT share_count FROM link_stat WHERE url = '".$url."'";
            $apifql = "https://api.facebook.com/method/fql.query?format=json&query=".urlencode( $fql );
            $json = file_get_contents( $apifql );
            $data = json_decode($json);
            $fbcount = $data[0]->share_count;
            update_post_meta($postid,'fb_shares',$fbcount);
            update_post_meta($postid,'share_time',current_time('timestamp'));
            return $fbcount;
        }
        else {
            return $fbcount;
        }

    }
    else {
        //If there is no result, save $fbcount in 'fb_shares' column with current timestamp
        $fql = "SELECT share_count FROM link_stat WHERE url = '".$url."'";
        $apifql = "https://api.facebook.com/method/fql.query?format=json&query=".urlencode( $fql );
        $json = file_get_contents( $apifql );
        $data = json_decode($json);
        $fbcount = $data[0]->share_count;
        update_post_meta($postid,'fb_shares',$fbcount);
        update_post_meta($postid,'share_time',current_time('timestamp'));
        return $fbcount;
    }
}

function getPlusOne($postid, $url){

    //Grab Results from gp_shares
    $gpcount  = get_post_meta( $postid, 'gp_shares', true );

    if( ! empty( $gpcount ) ) {
        $gbtime  = get_post_meta( $postid, 'share_time', true );
        //Compare timestamp with one hour ago
        if ($gbtime < current_time('timestamp') - 60*60){
            $html =  file_get_contents( "https://plusone.google.com/_/+1/fastbutton?url=".urlencode($url));
            $doc = new DOMDocument();   $doc->loadHTML($html);
            $counter=$doc->getElementById('aggregateCount');
            $gpcount = $counter->nodeValue;
            update_post_meta($postid,'gp_shares',$gpcount);
            update_post_meta($postid,'share_time',current_time('timestamp'));
            return $gpcount;
        }
        else {
            return $gpcount;
        }

    }
    else {
        //If there is no result, save $gpcount in 'gp_shares'
        $html =  file_get_contents( "https://plusone.google.com/_/+1/fastbutton?url=".urlencode($url));
        $doc = new DOMDocument();   $doc->loadHTML($html);
        $counter=$doc->getElementById('aggregateCount');
        $gpcount = $counter->nodeValue;
        update_post_meta($postid,'gp_shares',$gpcount);
        update_post_meta($postid,'share_time',current_time('timestamp'));
        return $gpcount;
    }
}

function getMyTweets($postid, $url){

    //Grab Results from social_count
    $tweetcount  = get_post_meta( $postid, 'tweet_shares', true );

    if( ! empty( $tweetcount ) ) {
        $tweettime  = get_post_meta( $postid, 'share_time', true );
        //Compare timestamp with one hour ago
        if ($tweettime < current_time('timestamp') - 60*60){
            $json = file_get_contents( "http://urls.api.twitter.com/1/urls/count.json?url=".$url );
            $ajsn = json_decode($json, true);
            $tweetcount = $ajsn['count'];
            update_post_meta($postid,'tweet_shares',$tweetcount);
            update_post_meta($postid,'share_time',current_time('timestamp'));
            return $tweetcount;
        }
        else {
            return $tweetcount;
        }

    }
    else {
        //If there is no result, save $tweetcount in 'tweet_shares' column with current timestamp
        $json = file_get_contents( "http://urls.api.twitter.com/1/urls/count.json?url=".$url );
        $ajsn = json_decode($json, true);
        $tweetcount = $ajsn['count'];
        update_post_meta($postid,'tweet_shares',$tweetcount);
        update_post_meta($postid,'share_time',current_time('timestamp'));
        return $tweetcount;
    }
}

0 个答案:

没有答案