You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中cache forget无法更新缓存,脚本清除缓存失效问题

缓存键无法在脚本中清除,但Tinker中正常的问题

我用缓存判断文件是否正在处理来避免并发访问,写了下面3个方法:

public function cacheKey(): string
{
    return "uploads.$this->filename";
}

public function checkSingleton(): bool
{
    if (cache($this->cacheKey())) return true;
    cache([$this->cacheKey() => 1], now()->addHours(2));
    
    return false;
}

public function clearSingleton(): void
{
    cache()->forget($this->cacheKey());
}

执行脚本时能正常设置缓存,但没法清除。奇怪的是,在php artisan tinker里执行相同操作,缓存键能正常删除。

我试过用cache()助手函数的cache()->forget(),也试过完整命名空间的Cache门面Cache::forget(),数据库里能看到缓存键已创建,但就是删不掉。

补充说明

我还试了原子锁方案,依然能创建锁但无法释放:

public function cacheKey(): string
{
  return "uploads.$this->filename";
}

public function checkSingleton(): bool
{
  return !Cache::lock($this->cacheKey(),60*60)->get();
}

public function clearSingleton(): void
{
  Cache::lock($this->cacheKey())->forceRelease();
}

可能的原因及解决方案

  • 缓存驱动/环境不一致
    脚本执行时可能未正确加载Laravel完整环境,导致使用的缓存驱动和Tinker不同(比如脚本用默认文件缓存,Tinker用数据库缓存)。确保脚本开头加载Laravel环境:
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

同时检查脚本的CACHE_DRIVER环境变量是否和项目配置一致。

  • 缓存键存在隐性差异
    确认脚本中$this->filename的值和Tinker测试的完全一致,包括大小写、空格、特殊字符。可以在clearSingleton()里打印缓存键对比:
public function clearSingleton(): void
{
    $key = $this->cacheKey();
    dump($key); // 输出键值,和Tinker中的结果对比
    Cache::forget($key);
}
  • 原子锁的上下文匹配问题
    使用Cache::lock()时,forceRelease()需要确保操作的锁键完全匹配,或者先检查锁是否存在再释放:
public function clearSingleton(): void
{
    $lock = Cache::lock($this->cacheKey());
    if ($lock->exists()) {
        $lock->forceRelease();
    }
}
  • 缓存前缀不匹配
    检查config/cache.php中的prefix配置,脚本可能因环境问题使用了不同前缀,导致实际缓存键和预期不符。可以打印带前缀的完整键对比:
dump(Cache::getPrefix().$this->cacheKey());

内容的提问来源于stack exchange,提问作者mankowitz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 08:55:18