Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does Laravel clear the cache?

I am trying to utilize Redis with Laravel 5 to cache queries/results to make my application run faster.

However, I have couple of questions which should help me decide when it is a good time to use Cache and when it is not.

Assume I have the following

$interview= Cache::remember('SomeUniqueKey', 180, function(){

    return SurveyInterview::where('user_id', 123)
                          ->with([
                                    'current_step.page',
                                    'pages'
                                ])
                          ->first();
});

The previous code should cache the collection set returned by eloquent. which is a total of 3 queries. First to query the model, second to query the current_step.page relation and the last one to query the pages relation.

It is my understanding that as long as I execute the above code over and over, Laravel will return the collection from the cache until the 180 minutes are up from the time the collection was cached.

Question

Will laravel automatically remove the cache if an update took place to my database tables? In other works, will it remove the cache once I make an update to my SurveyInterview model or will I have to remove that manually every time I update the model?

like image 838
Jaylen Avatar asked Jul 07 '16 16:07

Jaylen


People also ask

Where does php artisan cache clear?

To clear your application cache, you may run the following Artisan command: $ php artisan cache:clear Application cache cleared! This will clear all the cache data in storage which are typically stored in /storage/framework/cache/data/ .

How does Laravel cache work?

Laravel creates an encrypted file with the data and the cache key when new data is cached. The same happens when the user is trying to retrieve the content. Laravel cache searches through the folder for the specified key and, if found, returns the content.

Does Laravel view cache?

When a request is executed that renders a view, Laravel will compile the view if a pre-compiled view does not already exist (or if the view has been modified more recently than the compiled view). View caching pre-compiles all of the views utilized by your application.


1 Answers

The answer to this really depends on what cache driver is being used.

For drivers like Memcache or APC, those services know how to "garbage collect" themselves and automatically flush expired entries.

For the File driver, it relies on the the UNIX timestamp of the file creation to determine if it has expired. The file is not deleted until an attempt to retrieve it occurs and it is determined to be expired

For the Database driver, it stores an expiration column and can determine how if it is expired from that timestamp. The record is not automatically deleted upon expiration -- only when an attempt to get the cache entry occurs and it is found to be expired (similar to the file driver).

To answer the overarching question, you are correct that remember() will continue to retrieve it from the cache until it expires. It does that by calling the get() method on the specific storage class, which in the case of file and database, will flush the entries if expired automatically. When it returns no entry due to expiration, your closure logic will execute to generate a fresh set of data.

As far as I am aware, no, it will not flush the cache when an update occurs. A quick search of the Illuminate\Database folder for forget or flush does not indicate that it would do that. That being said, you can detect when an update occurs with model events and have it flush the cache fairly easily by adding something like this to the model.

public static function boot()
{
    SurveyInterview::updated(function ($survey) {
        app('cache')->forget('my-survey-key-' . $survey->id);
    });
}

References

FileStore: https://github.com/laravel/framework/blob/5.2/src/Illuminate/Cache/FileStore.php

DatabaseStore: https://github.com/laravel/framework/blob/5.2/src/Illuminate/Cache/DatabaseStore.php#L66

EDIT

Redis, like Memcache and APC, handle the flush on their own. For example, see this:

  • http://redis.io/commands/expire
  • https://github.com/laravel/framework/blob/5.2/src/Illuminate/Cache/RedisStore.php
like image 177
Jeremy Harris Avatar answered Sep 27 '22 21:09

Jeremy Harris