【发布时间】:2013-01-03 10:01:45
【问题描述】:
我一直在我的网站上使用基于 this link 的基本缓存系统
到目前为止,它对我想做的任何事情都很有效。
$cachefile = 'cache/'. basename($_SERVER['QUERY_STRING']) . '.html';
$cachetime = 1440 * 60;
if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) {
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->";
exit;
}
ob_start();
// My html/php code here
$fp = fopen($cachefile, 'w'); // open the cache file for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close
ob_end_flush(); // Send to browser
然而,我有几个页面包含更详细的 mysql 查询,我花了相当多的时间优化它,但是当我在 mysql 中查询它时,它仍然需要大约 10 秒才能运行,甚至在网站上运行更长时间。有时,当我收到以下消息时,它似乎超时了。
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the requestGET http://www.example.com
Reason: Error reading from remote server
这不是一个大问题,因为我使用的是上面的缓存系统,只有当天第一个点击它的人会延迟,其余时间用户会获得缓存页面,所以实际上是相当的对他们来说快。
我想让自己不必成为每天第一个访问该页面并自动执行此过程的人,以便在每天 17:00(在服务器上)将文件写入缓存。
我怎样才能最好地做到这一点?
【问题讨论】:
-
如果你有 linux 服务器 - cron 作业
标签: php performance caching