如何缓存/保存外部API [英] How to cache/save external API's

查看:58
本文介绍了如何缓存/保存外部API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究如何保存/缓存外部api,因为当外部api无法正常工作时,这会使我的网站充满错误.我想知道一种缓存API的方法. 我将以 http://steamcommunity.com/id/712?xml=1的示例为例 它通常会失败,我也将以json格式的api为例,但没有api密钥仍找不到任何api仍然是json api- http://api.steampowered.com/IEconItems_440/GetSchema/v0001/?key=&lang== 一些帮助将不胜感激 谢谢!

i was looking at how to save/cache external api because when the external api's are not working then it makes my website full of errors. i would like to know a way to cache api. i would take an example of http://steamcommunity.com/id/712?xml=1 it often goes down and i would also take an example of json formatted api's but couldn't find any api without an api key still this is an json api - http://api.steampowered.com/IEconItems_440/GetSchema/v0001/?key=&language=en Some help would really be appreciated Thanks!

推荐答案

您可以为此滚动自己的包装器.工作流程如下:

You can roll your own wrapper for that. The workflow as follows:

  • 检查缓存中是否存在获取的
  • 如果有,那么您将对其进行检索
  • 如果没有,那么您从所需的URL中获取所有内容并将其存储在缓存中

-看起来像这样,

$cache = new YourOwnCacheManager(array(
   'ttl' => 60,
   'path' => '/temp.cache'
));


$url = 'http://steamcommunity.com/id/712?xml=1'


if ($cache->isCached($url)) {

   $content = $cache->read($url);

} else {

   $content = file_get_contents($url);
   $cache->write($url, $content);
}

echo $content;

这篇关于如何缓存/保存外部API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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