thinkphp 8.0 缓存清理需按类型精准执行:php think clear --all仅清驱动级缓存;模板缓存须加-t参数;路由、配置、模型查询缓存须分别用clear:route、config:clear、db::clearcache();标签清理依赖驱动支持且redis需配tag_prefix。

ThinkPHP 8.0 的缓存清理不是“一键删目录”就完事,不同缓存类型有各自生效逻辑和清理路径。用错命令,可能表面清了,实际旧数据还在跑——尤其线上环境,得看清目标再动手。
先搞清你要清什么
TP8.0 缓存分好几层,命令默认只管其中一部分:
-
驱动级缓存(如 runtime/cache/ 下的文件、Redis 中带 prefix 的 key)→
php think clear --all能清 -
模板编译缓存(runtime/view/)→ 必须加
-t参数:php think clear --all -t -
路由缓存(runtime/route.php)→ 单独命令:
php think clear:route -
配置缓存(runtime/config.php)→ 单独命令:
php think config:clear或重建:php think config:cache -
模型查询缓存(如
UserModel::cache(true)->select()生成的)→ 不走通用缓存系统,需手动调\think\facade\Db::clearCache()
常用命令实操清单
这些命令都在项目根目录执行,确保你有运行权限:
- 清所有驱动缓存 + 模板编译缓存:
php think clear --all -t - 只清路由缓存(改了注解或 route.php 后必做):
php think clear:route - 清配置缓存并重建(改了 .env 或 config/app.php 后):
php think config:clear && php think config:cache - 清数据库字段结构缓存:
php think db:clear-cache(或在控制器中调\think\facade\Db::clearCache()) - 清某个标签下的缓存(如 user 标签):
php think cache:tag user clear(注意:仅 Redis/file 驱动支持,且 Redis 需配tag_prefix)
为什么清了还是有旧数据?常见卡点
不是命令没用,而是环境或逻辑没对上:
-
APP_DEBUG = true 时,大部分缓存自动失效 → 清了也白清,先确认
config/app.php或.env中APP_DEBUG=false -
缓存驱动其实是 null 或 file,但你以为是 Redis → 运行
php think optimize:config查看当前生效的default和stores -
runtime/ 目录不可写 → 命令静默失败,检查 Web 进程用户(如 www-data)对
runtime/有读写权限 -
用了自定义 store(如
Cache::store('my_redis')) →php think clear --all不会碰它,得显式调Cache::store('my_redis')->clear()
安全绕过缓存的临时方案
不确定要不要清、怕误操作?先绕过更稳妥:
- 临时禁用全部缓存:在
public/index.php顶部加define('THINK_CACHE', false); - 临时让 Redis 失效(不删数据):把
config/cache.php中 Redis 的host改成一个无效地址,框架会自动降级到 null 驱动 - 开发阶段快速验证:直接设
app_debug => true,所有缓存跳过,改完即生效
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











