在PHP开发中,缓存页面是一种提高网站性能的有效方法。通过缓存页面内容,可以减少服务器处理请求的次数,从而加快页面加载速度。以下是一个简单的PHP缓存页面实例,使用文件系统作为缓存存储。
| 步骤 | 代码实现 |

| --- | --- |
| 1. 创建缓存文件目录 | `mkdir('cache', 0777, true);` |
| 2. 创建缓存函数 | `function cache_page($url, $expire = 3600) {`
` $cache_path = 'cache/' . md5($url) . '.html';`
` if (file_exists($cache_path) && (time() - filemtime($cache_path)) < $expire) {`
` return file_get_contents($cache_path);`
` }`
` return false;`
`}` |
| 3. 在页面中使用缓存函数 | `if (!cache_page($url)) {`
` echo "







