fsdolphin's avatar

Cache Tutorials

Hi,

I'm currently trying to understand cache in Laravel ( and in general) but the only tutorial I have found here in Laracasts is one from 2013 and I was wondering if it still applies to the most current Laravel version.

Is there any good tutorial about cache for Laravel 5?

Is the following tutorial still worth watching (caching-essentials)?
https://laracasts.com/lessons/caching-essentials

Thanks a lot.

0 likes
10 replies
toniperic's avatar
Level 30

It is still perfectly valid.

The actual code for latest Laravel release will probably be somewhat different, but the concepts are the same, and that's what you need to get a grasp of.

1 like
fsdolphin's avatar

@toniperic Thank you for the info.

Quick question. As I stated in my original post, I'm new to caching. The way understand caching is that you tell Laravel what part of the database you want to cache (remember) and Laravel will save that content to a specific file and use that file whenever a query is made for that specific piece of information, right?

If the above statement is correct I believe there is an option to tell Laravel how long to use the cache file for, now, is there a way to override that time with a click of a button? In other words, if you tell Laravel to cache for 2 days but within that period the data is updated can you clear cache with a method call?

Thanks a lot!

toniperic's avatar

@fsdolphin cache will work however you implement it to.

Database caching is a different type of caching, but you can use Laravel caching in for any amount of time to avoid expensive database queries. Please keep that in mind - caching isn't only database related, but you can save data you would usually get from a database call, thus when you refer to it next time - the framework will first check if there is valid cached data and only query the database if there isn't any.

Please take a look at the official docs, more specifically the "Remember Or Update" section, because what you have described could easily be achieved with the Cache::remember() method which is described there.

And yes - of course you can clear the cache however you want - through terminal or a web interface (in case you make one for such purpose). You refer to either Cache::forget('key') or Cache::flush() methods. The latter one clears all cached data, so use with caution.

Hope I helped.

1 like
fsdolphin's avatar

@toniperic
First of all thank you for your comments.

Just to clarify on your statement...

Database caching is a different type of caching, but you can use Laravel caching in for any amount of time to avoid expensive database queries. Please keep that in mind - caching isn't only database related, but you can save data you would usually get from a database call, thus when you refer to it next time - the framework will first check if there is valid cached data and only query the database if there isn't any.

Here is how I'm planning on using it. I currently have a very simple CMS where I'm storing the content of the pages in a database so I would like to cache this content to minimize the quantities of queries made to the database.

Does that sound like something common where cache could be used?

The reason for my question is because you mentioned that Database caching is a different type of caching and I want to make sure I'm going in the right direction.

Thanks

toniperic's avatar

Yeah, just look at the Cache::remember() method, or rememberForever() method if that content barely ever changes.

For example if you do this

$value = Cache::remember('foo', $minutes, function() {
    // do your query here and return whatever you wish to cache
    return $something;
})

Then on every request your app will first check if there is a valid cache for 'foo' key. Valid meaning it hasn't expired. If you set $minutes variable to 2880 that will translate to two days. So, if there is a valid cache it will return it without querying the database (this is what you'd pass as third parameter in the callback function). Otherwise, if there isn't anything in the cache or it isn't valid, it'll run the callback and store the results for the next 2880 minutes (two days).

If you wish for retrieve items from cache really fast, you could use in-memory driver for cache, such as memcached. You'll get fastest loading times, and even if the server crashes no harm is done - all in-memory cache will be lost, but on the first page load it will get re-cached and everything will work smoothly again.

If you don't need to load cached data that fast, you can use the file cache driver that will write to the filesystem.

PS: when I said "Database caching is different" I mean the real database-specific caching that has nothing to do with Laravel or any other app or framework.

1 like
fsdolphin's avatar

@toniperic Thank you for all of your help.

Sorry but one more question. Is Redis another way of caching data? If yes, when would you use it instead of the built in caching system in Laravel?

toniperic's avatar

@fsdolphin yes, Redis is a service that lets you cache data. You would use it because retrieving and writing to memory is much faster than to filesystem (which makes I/O operations on your drive). So, you'd use it when the speed of your app is important.

1 like

Please or to participate in this conversation.