为什么我的缓存在使用 0 分钟时不会在 Laravel 4.2 过期

Why doesn't my cache expire on Laravel 4.2 when using 0 minutes

背景

Laravel 4.2 版通过调用 remember() 方法提供基于 QueryBuilder 级别的缓存。

文档建议,您可以使用 rememberForever()remember($minutes)

使用 remember(0) 时,我希望生成的缓存会立即过期或被记住不到一分钟。

问题

为什么下面的查询缓存不会立即过期?

$model = Model::remember(0)->where('id', 789)->first();

我正在使用 file 驱动程序。

Laravel 文档状态

Caching Queries You may easily cache the results of a query using the remember or rememberForever method:

$users = DB::table('users')->remember(10)->get();

In this example, the results of the query will be cached for ten minutes. While the results are cached, the query will not be run against the database, and the results will be loaded from the default cache driver specified for your application.

文档没有说明的是,如果您使用的是 file 驱动程序并且您通过了 0,那么您将遇到这行代码。

protected function expiration($minutes)
{
    if ($minutes === 0) return 9999999999;

    return time() + ($minutes * 60);
}

这意味着您将获得一个基于文件的缓存,其有效期为 2286 年 11 月 20 日星期六 17:46:39 +0000