2012-07-10 26 views
12

存在一些问题。一直玩脸书和推特API,并获得状态搜索查询的JSON输出没有问题,但是我已经进一步阅读,并意识到我最终可能会被引用文档中的“速率限制”。在PHP中缓存JSON输出

我想知道是否容易缓存每小时的JSON输出,这样我至少可以尝试并防止发生这种情况?如果是这样怎么办?当我尝试了一个YouTube视频,但并没有给出太多的信息,只有如何写一个目录列表的内容到一个cache.php文件,但它并没有真正指出这是否可以用JSON输出完成,当然也没有没有说如何使用60分钟的时间间隔或如何获取信息然后退出缓存文件。

任何帮助或代码将非常赞赏,因为在这种事情上的教程似乎很少。

回答

26

这里一个简单的功能,增加了缓存来获得一些URL内容:

function getJson($url) { 
    // cache files are created like cache/abcdef123456... 
    $cacheFile = 'cache' . DIRECTORY_SEPARATOR . md5($url); 

    if (file_exists($cacheFile)) { 
     $fh = fopen($cacheFile, 'r'); 
     $cacheTime = trim(fgets($fh)); 

     // if data was cached recently, return cached data 
     if ($cacheTime > strtotime('-60 minutes')) { 
      return fread($fh); 
     } 

     // else delete cache file 
     fclose($fh); 
     unlink($cacheFile); 
    } 

    $json = /* get from Twitter as usual */; 

    $fh = fopen($cacheFile, 'w'); 
    fwrite($fh, time() . "\n"); 
    fwrite($fh, $json); 
    fclose($fh); 

    return $json; 
} 

它使用URL来识别缓存文件,重复下一次将从缓存中读取对相同URL的请求。它将时间戳写入缓存文件的第一行,并丢弃超过一小时的缓存数据。这只是一个简单的例子,你可能想要自定义它。

+0

最后一个似乎很简单,但我怎么会在我的网站上使用它。我对PHP还是比较陌生的,只是在过去的一年里一直在学习,所以原谅我的noobness。我不确定$ json =/*会像往常一样从twitter获取* /;部分。我是否像这样使用它: – GeordieDave1980 2012-07-10 06:36:35

+0

我的代码: - 'code' $ query =“i%20hate%20my%20girlfriend”; $ query = urlencode($ query); $ url =“http://search.twitter.com/search.json?q={$query}&rpp=100&result_type=recent&lang=en”; $ tweet = file_get_contents($ url); $ tweet_array = json_decode($ tweet,true); foreach($ tweet_array ['results'] as $ data) { $ profileimg = $ data ['profile_image_url']; $ postfrom = $ data ['from_user_name']; $ postfromid = $ data ['from_user_id']; $ created = $ data ['created_at']; $ message = $ data ['text']; – GeordieDave1980 2012-07-10 06:44:15

+0

我已经对我的代码留下了评论,我不确定如何将其放入评论框,希望您能够复制并粘贴它并将其分开,以便知道它的样子。我对这个函数做什么感到困惑。 – GeordieDave1980 2012-07-10 06:45:48

4

使用缓存来避免速率限制是个好主意。 下面是一些示例代码,展示了我如何在Google+数据中使用它, 在最近编写的一些php代码中。

private function getCache($key) { 
    $cache_life = intval($this->instance['cache_life']); // minutes 
    if ($cache_life <= 0) return null; 

    // fully-qualified filename 
    $fqfname = $this->getCacheFileName($key); 

    if (file_exists($fqfname)) { 
     if (filemtime($fqfname) > (time() - 60 * $cache_life)) { 
      // The cache file is fresh. 
      $fresh = file_get_contents($fqfname); 
      $results = json_decode($fresh,true); 
      return $results; 
     } 
     else { 
      unlink($fqfname); 
     } 
    } 

    return null; 
} 

private function putCache($key, $results) { 
    $json = json_encode($results); 
    $fqfname = $this->getCacheFileName($key); 
    file_put_contents($fqfname, $json, LOCK_EX); 
} 

,并使用它:

 // $cacheKey is a value that is unique to the 
     // concatenation of all params. A string concatenation 
     // might work. 
     $results = $this->getCache($cacheKey); 
     if (!$results) { 
      // cache miss; must call out 
      $results = $this->getDataFromService(....); 
      $this->putCache($cacheKey, $results); 
     } 
+0

getCacheFileName($ key)的代码是什么? – simpatico 2016-11-26 16:06:50