在php网站开发中,缓存是提升性能的核心手段之一,但缓存数据过期、更新后未同步清除都会导致页面显示异常,因此掌握缓存设置与清除方法是开发者的必备技能。合理的缓存管理既能减少服务器资源消耗,也能保证用户看到最新的内容。

常见的php缓存管理策略
缓存管理策略需要根据业务场景选择,不同策略对应不同的清除逻辑,以下是三种主流的php缓存管理策略:
| 策略类型 | 适用场景 | 清除逻辑 |
|---|---|---|
| 定时过期策略 | 新闻列表、商品详情等更新频率固定的页面 | 给缓存设置固定的过期时间,到期自动失效 |
| 事件触发策略 | 后台编辑内容后需要立即更新前端展示的场景 | 内容更新时主动触发对应缓存的清除操作 |
| 版本号控制策略 | 静态资源、接口返回数据等需要版本管理的场景 | 更新内容时修改缓存键名的版本号,旧缓存自动失效 |
php主流缓存类型的手动清除方法
1. 文件缓存手动清除
文件缓存是php中最基础的缓存方式,通常将缓存数据存储在服务器指定目录的文件中,手动清除时直接操作缓存目录即可。
以下是文件缓存的设置与手动清除示例代码:
<?php
// 文件缓存类
class FileCache {
private $cacheDir;
// 构造函数,指定缓存目录
public function __construct($dir = './cache/') {
$this->cacheDir = rtrim($dir, '/') . '/';
if (!is_dir($this->cacheDir)) {
mkdir($this->cacheDir, 0755, true);
}
}
// 设置缓存,默认过期时间1小时
public function set($key, $value, $expire = 3600) {
$file = $this->cacheDir . md5($key) . '.cache';
$data = [
'expire' => time() + $expire,
'value' => $value
];
file_put_contents($file, serialize($data));
}
// 获取缓存
public function get($key) {
$file = $this->cacheDir . md5($key) . '.cache';
if (!file_exists($file)) {
return null;
}
$data = unserialize(file_get_contents($file));
if (time() > $data['expire']) {
unlink($file);
return null;
}
return $data['value'];
}
// 手动清除单个缓存
public function delete($key) {
$file = $this->cacheDir . md5($key) . '.cache';
if (file_exists($file)) {
unlink($file);
}
}
// 手动清除所有缓存
public function clearAll() {
$files = glob($this->cacheDir . '*.cache');
foreach ($files as $file) {
unlink($file);
}
}
}
// 使用示例
$cache = new FileCache();
// 设置缓存
$cache->set('user_list', ['user1', 'user2'], 1800);
// 清除单个缓存
$cache->delete('user_list');
// 清除所有缓存
$cache->clearAll();
?>2. Redis缓存手动清除
Redis是php项目中常用的高性能缓存中间件,支持多种数据结构,清除方式也更灵活。
以下是Redis缓存的设置与手动清除示例代码,需要提前安装php的redis扩展:
<?php
// Redis缓存操作类
class RedisCache {
private $redis;
// 构造函数,连接Redis
public function __construct($host = '127.0.0.1', $port = 6379) {
$this->redis = new Redis();
$this->redis->connect($host, $port);
}
// 设置缓存,默认过期时间1小时
public function set($key, $value, $expire = 3600) {
$this->redis->setex($key, $expire, serialize($value));
}
// 获取缓存
public function get($key) {
$data = $this->redis->get($key);
return $data ? unserialize($data) : null;
}
// 手动清除单个缓存
public function delete($key) {
$this->redis->del($key);
}
// 手动清除指定前缀的所有缓存
public function clearByPrefix($prefix) {
$keys = $this->redis->keys($prefix . '*');
if (!empty($keys)) {
$this->redis->del($keys);
}
}
// 手动清除所有缓存(生产环境谨慎使用)
public function clearAll() {
$this->redis->flushAll();
}
}
// 使用示例
$cache = new RedisCache();
// 设置缓存
$cache->set('article_1', ['title' => 'php缓存教程', 'content' => '缓存设置方法'], 7200);
// 清除单个缓存
$cache->delete('article_1');
// 清除前缀为article_的所有缓存
$cache->clearByPrefix('article_');
?>3. Opcache手动清除
Opcache是php内置的字节码缓存,用于缓存编译后的php脚本,提升脚本执行效率,清除Opcache需要调用对应的内置函数。
以下是Opcache手动清除的示例代码:
<?php
// 清除所有Opcache缓存
if (function_exists('opcache_reset')) {
opcache_reset();
echo 'Opcache缓存清除成功';
} else {
echo '当前环境未开启Opcache';
}
// 清除指定文件的Opcache缓存
$file = '/www/wwwroot/test.php';
if (function_exists('opcache_invalidate')) {
opcache_invalidate($file, true);
echo '指定文件Opcache缓存清除成功';
}
?>缓存自动清除的配置要点
除了手动清除,还可以通过配置实现缓存自动清除,减少人工操作成本:
- 文件缓存可以在设置时指定过期时间,php读取时判断过期自动删除,也可以配合定时任务定期清理过期缓存文件
- Redis缓存设置时直接指定
expire参数,到期会自动失效,也可以配置Redis的最大内存策略,内存不足时自动淘汰旧缓存 - Opcache可以在php.ini中配置
opcache.force_restart_timeout参数,定期自动重启清除缓存,也可以在代码更新后通过脚本自动调用opcache_reset函数
在实际项目中,建议结合业务场景混合使用多种缓存管理策略,既保证缓存效率,也能在内容更新时及时清除对应缓存,避免用户看到过期内容。