How does the Laravel 4 Query Cache work?

6

I'm trying to cache my query, but I do not know how to recover the data later. Another question: when the number of rows in the database increases, does it automatically redo the query cache, or every time I enter the page does it cache the query again?

$value = Cache::remember('users', 50, function()
{
    return DB::table('users')->with('ucomments')->get();
});
    
asked by anonymous 12.01.2014 / 07:22

2 answers

6

When you use the remember method, the created cache is only maintained by the number of minutes passed in the $minutes parameter:

$value = Cache::remember('users', $minutes, function()
{
    return DB::table('users')->with('ucomments')->get();
});

As you passed 50 , after 50 minutes the cache will be rewritten with the result of a new query to the users table.

Notes:

  • As long as the indicated number of minutes does not pass, the cached query will not be updated.

Collect

To collect the users key from the cache, you can use:

$value = Cache::get('users');

Interesting:

12.01.2014 / 16:24
2

I usually use the remember ($ min) method in the propiria query.

DB::table('users')->with('ucomments')->remember(10)->get();

The laravel will store a cache of your query for 10 minutes.

I find this the most practical and easy method.

Laravel documentation for query cache

    
14.01.2014 / 22:18