This episode is for members only

Sign up to access "Laravel Performance" right now.

Get started
Already a member? Sign in to continue
Playing
12. Clearing cached data by tags

Transcript

00:00
So before we look at clearing out a cache effectively, particularly when we have keys like this, in the last episode I said that these were minutes, they're actually seconds.
00:09
So the data that we're storing here is cached for 60 seconds. Again, you can apply the same concept here by multiplying this by how many hours you want, or minutes you want, it's entirely up to you. We'll leave this at 60 seconds just for now while we demonstrate this.
00:24
In fact, let's bump this up to 10. So we're basically caching this for 10 minutes now. Okay, so we want to get around the fact that new posts will be published on our blog, but of course we don't want to have to manually come in,
00:39
clear this cache out every single time to see new posts. That wouldn't be great, it would take too much time, and it also clears out your entire cache as well. So what we're going to do is we're going to make use of cache tags to do this effectively.
00:54
And the reason that we're using cache tags is, first of all, that makes it a lot easier to clear out specific parts of your application, but also we can clear out things like this where we have lots of different page requests for the pagination and also the user ID in the request as well.
01:10
Okay, so to use tags, we need to switch over the driver. At the moment, we are using the file driver. And if we try and tag this, so let's tag this under a key called posts. So this just covers posts in general, and we come over and give this a refresh.
01:25
You can see this cache store does not support tagging. Now I have Redis enabled on my machine, so I can actually switch this over to Redis now, and you can see that this works nicely. And notice we actually get a fairly shorter request duration as well
01:39
when we're using the Redis cache store as well. It's a lot faster than pulling this data from a file. So now that we've switched over to Redis or another cache driver that supports tags, we can effectively clear out all of the posts,
01:52
any posts anywhere in our app when new content is published. So how do we do that? Well, we already saw before that we can use the cache facade or the cache helper to go ahead and forget these or flush them under a specific key.
02:06
But we can also do this under tags as well. So if we want to flush out all posts, we just use this. If we come over to the homepage now and give this a refresh, we get the same thing that we saw in the last episode,
02:16
where nothing is now cached because we're just clearing out every single time. So we can use this to automatically flush all posts from our cache under this tag when we publish a new article. How do we do that?
02:30
Well, there are lots of different ways. We can use an observer. We can use an eloquent event within our model. Let's go ahead and create an observer
02:37
because I find these a little bit easier to work with. So we're going to go and make an observer in here and we're going to call this post observer. And we can pass the model through here if we want to
02:48
or we can just go ahead and leave it blank. So we're going to go over to the post observer, which is now under app and observers, and we're going to create out a method in here.
02:56
In fact, let's just restructure this using the model and set that to post and then we'll force this. And you can see it gives you the scaffolding for each of the events for a post. So what we want to do is utilize the created event to say,
03:12
well, when a post is created, we want to clear the cache. We could even do that for when the post is updated as well. We're going to get the rest of these. We could even do it for deleted.
03:21
That probably makes sense. Let's just do these two for now, just to make things a little bit shorter. OK, so how do we register this observer on our post?
03:30
Well, we can do this over in app service provider. We could do this in event service provider. I'll do it in app service provider now. We're going to go ahead and say post observe
03:39
and we're going to choose the post observer, just the fully qualified class name to this. Pull in the namespace and we're done. So now when a post is created, this will be invoked.
03:50
So let's just die dump and say created and we can do the same for updated as well in a minute. So let's create a new post. We can just do this on the command line using Tinker.
04:00
So I'm going to say post factory and create and we're going to do this for a particular user. I'm actually just going to pass in the user ID manually here. We could do this in a better way, but let's just make it easy.
04:13
And there we go. You can see that we get created dumped out here. So that's the event hooked up. Now what we can do is just pull in what we did before.
04:21
When a post is created, flush out the cache of all posts. So we see that with that new data. So we're going to do that for created. We're going to do that for updated.
04:32
We could do that for deleted as well if we wanted to. You might even not want to do this. And if your cache time is short enough and you're happy to wait for the post to appear
04:41
in the cache when it's published, that's absolutely fine as well. So let's go over and just give this a refresh. You can see that we have this cache now.
04:49
That's cached for, how long do we do that for? Let's check it out, 10 minutes. So it's sure enough. We're not going to see any new data roll in here.
04:58
Let's go ahead and create a new post. And let's just do this over on the command line again. So we'll pull up the user factory. No, let's do post factory and create.
05:11
And we'll create that for a user ID of one. So when we do that, that's now taken that cache, flushed it out under that tag. And when we give this a refresh,
05:23
sure enough, we see that at the top. But then the next time we access the page, it's cached again. So like I said, caching can be very, very complicated.
05:31
But for simple lists of data like this, this technique is great for 80% of the time. We show a list of posts, we cache them. When a new one is created, we clear the cache out.
05:42
And then as long as a new cache isn't created and it doesn't expire, we're going to see the list of cached data rather than pull this from the database.
15 episodes1 hr 9 mins

Overview

Let's keep our Laravel applications feeling snappy! In this course, we cover the absolute fundamentals you need to keep in mind when building anything with Laravel.

While Laravel handles a lot for you, it's easy to fall into the trap of not considering and monitoring performance as you go. Keep these tips in your toolbelt, and you'll be able to develop faster apps, from the beginning.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!