One of the most important things to keep in mind when creating a website is to make the website fast. You don’t want your users to need to wait for your website to be loaded, especially on slower mobile internet connections. You can speed up your website in various ways and one of them is caching. Caching can be done on application code level with for example Redis or Memcached. But it’s also possible on server level with either Nginx or Apache. In this short blog post I will tell you more about static cache and how it can be setup using a package.
What is static cache
Static caching converts the page that is generated by a users's request into an HTML document that's served in any subsequent requests to that same page. Rather than the server interpreting the site code itself, maybe even querying the database, and returning an HTML document back to the user, static caching saves the result in an HTML document and provides that document to anyone else making the request. When serving truly static pages it isn't needed to boot up Laravel just to serve a static page. You can imagine that this saves a lot of time because we can immediately serve the HTML document and don't even need to touch PHP. Serving a simple HTML page from disk is infinitely faster and less intensive for the server.
Difference between static and dynamic content
So static cache is in fact any file that will be stored on a server and contains the same content every time it is delivered to the user. For example the HTML file mentioned in the previous paragraph. Static content is like the news: once the news is released, it contains the same content for everyone who reads it. No matter what comes up in the next news item.
Unlike the news, the weather forecast is more like what dynamic content is. It changes based on factors specific to the location of where you are. The weather forecast isn't the same for every location.
When you can and can't use static cache
First of all you don't want to cache every response. You only want successful
GET requests that return an HTTP 200 status as response to be cached. Obviously requests that use a
DELETE method shouldn't be cached because this usually contains user input.
Also you can't cache when using forms on a page. The cached version of the page may have captured some of the dynamically generated data from a previous submission. That’s a problem, especially if it’s data that the user doesn’t see. Like in hidden fields, or if it’s in a field that you are preventing the user from editing. For instance, you may be using a URL parameter to populate an email field that the user doesn’t see.
How to make use of static cache
Luckily there are a few packages that can help you with setting up static cache for your Laravel website. One of the packages I use myself is josephsilber/page-cache. The package has an extensive and clear README which explains how to install and configure it with either Apache or Nginx. Using the middleware included in this package, you can decide yourself which the disk the cached response should be saved to.