在WordPress中缓存自定义社交共享计数 [英] Caching custom social share count in WordPress

查看:75
本文介绍了在WordPress中缓存自定义社交共享计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很喜欢在我的博客文章上有一个共享柜台。我注意到它实际上鼓励访问者自己共享内容。因为那里没有我真正感到满意的WordPress sharecount插件(它们中的大多数都能让很多人打电话),所以我自己编写了代码。

I really like having a share counter on my blogposts. I noticed that it actually encourages visitors to share the content themselves. Because there are no WordPress sharecount plugins out there that I actually find satisfying (most of them make way to much calls), I wrote the code myself.

它的工作原理非常完美,但仍然会使我的网站变慢。因此,我希望它每小时缓存和刷新一次。我不知道该如何管理………有什么想法吗?

It works perfect, but still slows down my site. So I would rather it caches and refreshes once per hour or so. I don't know how to manage this though … Any ideas?

这是我放入主题功能文件中的内容:

This is what I put in the themes function file:

class shareCount {
private $url,$timeout;
function __construct($url,$timeout=10) {
$this->url=rawurlencode($url);
$this->timeout=$timeout;
}

function get_tweets() { 
$json_string = $this->file_get_contents_curl('http://urls.api.twitter.com/1/urls/count.json?url=' . $this->url);
$json = json_decode($json_string, true);
return isset($json['count'])?intval($json['count']):0;
}

function get_fb() {
$json_string = $this->file_get_contents_curl('http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls='.$this->url);
$json = json_decode($json_string, true);
return isset($json[0]['total_count'])?intval($json[0]['total_count']):0;
}

private function file_get_contents_curl($url){
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
$cont = curl_exec($ch);
if(curl_error($ch))
    {
        die(curl_error($ch));
    }
        return $cont;
    }

}

这就是我在single.php中使用的:

And this is what I use in single.php:

<!-- Begin mod: Add share counter -->
<span class="share-count">
    <?php 
    $obj=new shareCount(get_permalink( $post->ID ));  
    echo $obj->get_tweets() + $obj->get_fb();
    ?>
</span>
<span class="share-text">
    keer gedeeld
</span>
<!-- End mod: Add share counter -->

然后我还添加了一些CSS。

Then I also add some css.

推荐答案

就像维森特说的那样,您应该使用内置的瞬时缓存。

Like vicente said, you should use the built in transient cache.

private function file_get_contents_curl($url){
    // Create unique transient key
    $transientKey = 'sc_' + md5($url);

    // Check cache
    $cache = get_transient($transientKey); 
    if($cache) {
        return $cache;
    }

    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
    $cont = curl_exec($ch);
    if(curl_error($ch))
    {
        die(curl_error($ch));
    }

    // Cache results for 1 hour
    set_transient($transientKey, $cont, 60*60);

    return $cont;
}

这篇关于在WordPress中缓存自定义社交共享计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆