【发布时间】:2015-05-09 03:14:57
【问题描述】:
我正在使用 PHP Simple HTML DOM Parser 并使用 $html->load_file
include('simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file('https://www.google.com/finance?q=goog');
下面的链接演示了一种强制缓存内容的方法。我需要根据 url 的最后部分检索内容更改的页面。是否可以修改下面的代码以强制缓存,例如,如果我检索以下页面:
google.com/finance?q=goog
google.com/finance?q=wwwfx
google.com/finance?q=vigrx
我可以将这些页面中的每一个缓存到会话结束吗,因为我将重复需要页面内容。
Caching PHP Simple HTML DOM Parser
function cache_get_contents($url, $offset = 600, $override = false) {
$file = '/tmp/file_cache_' . md5($url);
if (!$override && file_exists($file) && filemtime($file) > time() - $offset)
return file_get_contents($file);
$contents = file_get_contents($url);
if ($contents === false)
return false;
file_put_contents($file, $contents);
return $contents;
}
【问题讨论】:
-
这是一个懒人缓存。查看 memcached。或者编写代码查看文件的年龄,并在 n 小时后更新内容。
标签: php dom caching simple-html-dom