filecachinglaravel-4.2

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


Background

Laravel Version 4.2 offers caching based at the QueryBuilder level using a call to the remember() method.

The documentation suggests, you can either use rememberForever() or remember($minutes)

When using remember(0) I would expect the resulting cache to expire immediately or be remembered for less than a minute.

Question

Why does the below query cache not expire straight away?

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

I'm using the file driver.


Solution

  • The Laravel Docs state

    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.

    What the documentation fails to state is that if you are using a file driver and you pass 0 then you'll hit this line of code.

    protected function expiration($minutes)
    {
        if ($minutes === 0) return 9999999999;
    
        return time() + ($minutes * 60);
    }
    

    Meaning you will get a file based cache with an expiration of Sat, 20 Nov 2286 17:46:39 +0000