我有一个函数,生成一个包含来自DB的内容的表.有些单元格有自定义HTML,我正在通过模板系统读取file_get_contents.
小内容是相同的,但这个动作可能执行15次(我每页限制15个表行).如果file_get_contents看到内容相同,那么它也会缓存吗?
file_get_contents()没有缓存机制。但是,您可以使用编写自己的缓存机制。
这是一个草稿:
$cache_file = 'content.cache';
if(file_exists($cache_file)) {
if(time() - filemtime($cache_file) > 86400) {
// too old , re-fetch
$cache = file_get_contents('YOUR FILE SOURCE');
file_put_contents($cache_file, $cache);
} else {
// cache is still fresh
}
} else {
// no cache, create one
$cache = file_get_contents('YOUR FILE SOURCE');
file_put_contents($cache_file, $cache);
}
Run Code Online (Sandbox Code Playgroud)
UPDATE之前的if情况不正确,现在通过与当前时间进行比较来纠正。谢谢@Arrakeen。
| 归档时间: |
|
| 查看次数: |
13914 次 |
| 最近记录: |