如何缓存/保存外部 API's [英] How to cache/save external API's
问题描述
我正在研究如何保存/缓存外部 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=&language=en一些帮助真的很感激谢谢!
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:
- 检查您获得的 a 是否存在于缓存中
- 如果是,那么您将检索它
- 如果没有,那么您从所需的 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's的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!