我使用下面的代码来缓存访问过的URL,希望能节省一些CPU。在php文件的开头:
$url = $_SERVER['DOCUMENT_ROOT'].$_SERVER["REQUEST_URI"];
$break = Explode('/', $url);
$file = $break[count($break) - 1];
$cachefile = 'cache/cached-'.$file.'.html';
$cachetime = 900;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
// echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
readfile($cachefile);
exit;
}
ob_start();
字符串
在php文件的末尾:
// Cache the contents to a cache file
$cached = fopen($cachefile, 'w');
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush(); // Send the output to the browser
型
这工作正常,但它创建的文件使用完整的URI(即包括查询,#等)。我试图有缓存文件写一次,没有其他任何东西。
换句话说,当有人访问mysite.com/index.php时,我希望创建一个文件index.php.html。如果其他人访问mysite.com/index.php?sdgd=3534&#whatever&other&stuff,我希望缓存的文件仍然是index.php.html,从而覆盖先前创建的index.php.html。我不希望缓存的文件名为index.php?sdgd= 3534&#whatever & other & stuff. html--现在似乎发生了这种情况。
我知道这大概跟:
$cachefile = 'cache/cached-'.$file.'.html';
型
但我自己也想不出来。
如何做到这一点?
1条答案
按热度按时间ggazkfy81#
谢谢你们的建议。事实并非如此,所以我会回答我自己的问题。显然,答案就像改变一样简单
字符串
成
型
这似乎缓存了一个文件,其名称不包括查询。