تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Inspecting Cache Middleware0:00

Now that I understand how we fetch the documentation for each version, we parse it, we render it on the page, I'm still curious if there's any additional performance considerations here. So I'm going to go into my app directory, into Http, into Middleware, and immediately I see this cacheResponse.php file. This is not included by default with Laravel. So if we go to our Kernel, we can see it's included as part of every request. Alright, so let's take a look at this. cacheResponse, alright. So let's just quickly try to read it and then we'll go through it together.

Building Cache File Path1:44

the rest of the week you can cache this stuff pretty aggressively. Alright, let's see. We know how to determine if we should cache. So now we need to cache the response. Alright, so the first step, let's just go through it together. We get the directory and file names. Okay, so get the names of the directory and the file. And we can see here, we're grabbing the pathinfo, so let's just do this together. So we're getting this section right here. And we can see we are left trimming it, so we're getting rid of that forward slash there.

So we're getting this section right here. And we can see we are left trimming it, so we're getting rid of that / there. And then we are exploding it using a / as a divider. So that should give us an array of docs and 5.6, is that right? Let's see. Refresh, and there you go. Okay, so I assume it's using this to build up the file that we ultimately cache to. Let's see. Yeah, okay, so the file name will be ArrayPop, that's going to take the latest one off of the array.

Yeah, okay, so the file name will be ArrayPop, that's going to take the latest one off of the array. And it's going to attach .html. Alright, so we should have 5.6.html. Alright, so now we have a file that we will cache to. So what are we returning? We're returning an array that contains two items here. So one is that file we just created, so 5.6.html. But then the first item in the array, let's see. Alright, so we're going to implode the segments back into an array.

But then the first item in the array, let's see. Alright, so we're going to implode the segments back into an array. So I always like to go through this one step at a time. Okay, so yeah, okay, so we popped off 5.6 from that array. So now the array only contains docs or, you know, whatever else we might have, authorizations. Yeah, like that. So the file variable will now be authorization.html, because that's the last item in the array. And then the rest of the segments will be docs/5.6. Okay, so we're going to get cachePath with that value. Alright.

Okay, so we're going to get cache path with that value. Alright. Yeah, so I guess we're building up the path to where we will cache it. And that will be the page cache folder in our public directory. Let's take a look. And you can already see it there. So page cache, and then let's see, we're saying, do we have a path? Yes, we do. If so, trim off any / at the end, but then add it again. Hmm.

If so, trim off any / at the end, but then add it again. Hmm. I'm assuming that's just to make sure there aren't any situations where you end up with two /s or something like that. I'm not sure, but I assume it's related to that. Otherwise, if there is no path, echo that out there. Hmm. Okay. So that means we'll get a path to pageCache / docs / 5.6. And there it is.

So that means we'll get a path to page_cache/docs/5.6. And there it is. So we can see the rendered response here. So let's swipe back up. We now know how to cache the response. So let's see. We figured out the directory and file names. So now at this point, we should have an array that contains a path to where we are caching this documentation, and then another item for the file name itself. Okay.

Writing Cached HTML Files4:53

this documentation, and then another item for the file name itself. Okay. So here, this file, Laravel's just deferring to its fileSystem class. And in fact, if you want, you could even use the facade if you prefer. It's the exact same thing. So you'll see constructor injection here. We are injecting the fileSystem class. This one right here. And it has all the methods you're used to. get a file.

And it has all the methods you're used to. Get a file. And in this case, make a directory. Okay. Anyways. So let's switch back. We are making a directory for the path. So we're just ensuring that this full nested directory tree exists. And then we set the permissions. First we put to this path /authorization.html.

And then we set the permissions. First we put to this path /authorization.html. And specifically, we want to grab the content from the response. So yeah, you probably know this already. But once we have rendered the response, we can fetch the inner content, the full html there. So we're grabbing that html and throwing it into this file. So let's try... Do I have a dump here? No.

And in fact, it's going to be replaced every single time. So hello. We load the page and it's gone. And that makes sense if you think about it. Because what we said is, well, this middleware is going to run for every single page request. And right here we're saying, well, if you're making a GET request to a page that exists, then we want to cache it. So it begs the question, well, how is this ever being used? Because every time the page loads, we're just writing to the cache again. And further, let's do a find in folder.

Clearing Cache via Artisan6:44

Because every time the page loads, we're just writing to the cache again. And further, let's do a find in folder. I'm just going to look for any reference to that page cache folder. Okay. So here we are ignoring it. And then down here is the cache response middleware that we've been looking at. Now it looks like there's also an artisan command. Okay. So let's do php artisan. And it looks like right here under docs there is a command to clear the cache.

So let's do php artisan. And it looks like right here under docs there is a command to clear the cache. So let's see what happens. If we run that, page cache directory cleared. Okay. So that should entirely empty out this folder, and it does. However, as soon as we come back and load a page, it will start building that up once again. Okay. So let's quickly go through this, and then we'll rewind back to our original question.

Okay. So let's quickly go through this, and then we'll rewind back to our original question of, at what point is this ever being used? So we'll scroll down. The name of the command is php artisan config:clear. Whenever you want to add a namespace, you will precede it like so. And that will place it, if we scroll up, under this namespace here. Okay. So handle. So get the public_path.

So handle. So get the public path. So we're just figuring out where the page cache directory is. We will then clear the directory. That will just load the File system class once again through Laravel's service container and call the deleteDirectory method. And that's it. If that didn't return a falsy value, we echo out a... Let's do it one more time. That will echo out a message here.

Finding Upstream Implementation8:30

So we're going to head over to the GitHub repo and see if we can find any discussions related to this. Okay. So I'm going to look for... I don't know what I'm looking for. I could use git blame. However, after some recent PSR 2 commits that basically overwrite every file, it becomes a little more difficult to do that. But I think we should be okay. So let's look through issues.

But I think we should be okay. So let's look through issues. Okay. It looks like Joseph Silber maybe did this. page cache. Hmm. Yeah. I think this is it. Yeah. So here's the php artisan clear:cache command.

Yeah. So here's the clear page cache artisan command. Looks like he got rid of the default inspire command. He added this to the list of artisan commands. We updated the default middleware that we'll run. And then here's the main cache response middleware. All right. So let's read this. So now we can learn a little bit more. So this caches all requests as static files on the file system so that subsequent requests

How Static Cache Serves10:01

So does that make sense? If we're able to load it from the cache, it will read that file, return that response, and never actually hit the main index.php file where we go through the route, through the controller, through the documentation file, and all of that stuff. And if we scroll down, it has a note about the artisan command. And that's it. It got merged in about a year and a half ago from the time of this recording. And that's it. So now we fully understand how this works. We can even implement it into our own projects if we want to.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟