2013-10-25 3 views
0

블로그에 홈 페이지에 소셜 카운터가 포함되어 있습니다. 블로그는 10 개의 게시물과로드를 무한 스크롤로 더 보여줍니다.사회 카운터가 느린로드 발생

게시물당 3 개의 요청을 처리해야하며 10 개의 게시물을 표시해야한다는 점을 고려하면 페이지가 표시되기 전에 30 개의 요청을 거쳐야한다는 것을 의미합니다.

get_transient/set_transient를 사용하면 페이지가 즉시로드되지만 1 시간마다 1 명이 나쁜로드 시간을 갖게됩니다. 변수가 변경된 경우 jQuery로 확인하여 더 빨리 만들 수 있는지 궁금합니다 (if 그들은 가지고 있고, 그 후에 페이지 짐 후에 요구를한다).

jQuery에 대해 전혀 알지 못합니다. 이에 대한 도움을 주시면 감사하겠습니다.

function getShares($url){ 
    $fbcount = get_transient('share_count'); 
    if ($fbcount !== false) return $fbcount; 
    $fbcount = 0; 

    $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; 
    set_transient('share_count', $fbcount, 60*60); 
    return $fbcount; 
} 

function getPlus1($url) { 
    $gpcount = get_transient('plusone_count'); 
    if ($gpcount !== false) return $gpcount; 
    $gpcount = 0; 

    $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; 
    set_transient('plusone_count', $gpcount, 60*60); 
    return $gpcount; 
} 

function getTweets($url){ 
    $tweetcount = get_transient('tweet_count'); 
    if ($tweetcount !== false) return $tweetcount; 
    $tweetcount = 0; 

    $json = file_get_contents("http://urls.api.twitter.com/1/urls/count.json?url=".$url); 
    $ajsn = json_decode($json, true); 
    $tweetcount = $ajsn['count']; 
    set_transient('tweet_count', $tweetcount, 60*60); 
    return $tweetcount; 
} 
+0

게으른 로딩 플러그인을 사용하는 것이 더 좋습니다 .. – Daya

+0

나는 카운트 주변의 아이콘을 사용자 정의 할 수 있기 때문에 이런 방식으로하고 싶었습니다. 다른 플러그인으로 나는 그 어떤 옵션도 보지 못했습니다. –

답변

0

결과를 데이터베이스에 저장하고 결과가 한 시간 이상 경과하면 다시 다운로드하십시오.

(물론 새로운 데이터로 다시 데이터베이스에 저장하십시오.)

이 경우에도 jQuery가 필요하지 않습니다.

+0

나는 일시적으로 get/set을 사용하여 SQL을 사용하여 데이터를 저장한다는 인상을 받았다. 캐시는 현재 괜찮 으면 좋겠지 만, 매 시간마다 한 명의 사용자에 대해 궁금해하고 있습니다. –

+0

속도가 빠르지 만 걱정하지 마세요. – matiit