For the majority of websites, a great amount of the content doesn’t change from day to day or even month to month. Any time a user visits a website it will request the same resources and the web server will process the request and eventually retransmit to the client regardles of whether it’s been modified or not.
For a really busy website, as you can imagine, this can really begin to amount to a lot of really needless requests if the same people are visiting your website regularly. This isn’t really an efficient use of resources, and it’s really costly because you’ll need to keep adding more and more hardware to deal with the load. Thankfully, most of the applications that are used to host websites can handle this by the use of caching.
What is Caching?
A cache is software that is used to temporarily store data for more efficient usage. Caches can dramatically reduce the cost of web hosting and dramatically improve the user experience for web browsers or mobile apps.
By serving preprocessed content and telling the web browsers to temporarily store it, it’s possible to dramatically reduce the server. We do this by telling the “client” to only ask for the content when it’s been modified or when a certain amount of time has been asked.
What is Apache?
For those not aware, Apache is one of the most used pieces of web server software on the internet. It powers at least 40% of the websites on the internet and has been around since before 2000.
The type of caching that can be setup in Apache is going to depend on the resources you have available on your server. There’s two flavours of caching that can be done in Apache without having to really do any large changes to the system.
I recommend starting with serving from disk, it’s the cheapest option and basically everyone can do this in a few minutes.
In the Apache Server Configuration you’ll need to find the following line and uncomment it by removing the # at the sart of it.
LoadModule cache_module modules/mod_cache.so
For disk caching you’ll need to also find this line and uncomment it in the Apache Server Configuration.
LoadModule disk_cache_module modules/mod_disk_cache.so
For a WordPress site, we would probably add the following lines to the Apache Server Configuration we need to also add the following lines.
CacheEnable disk / CacheRoot /webapps/cache/app1 CacheDefaultExpire 3600 CacheDisable /wp-admin
CacheDefaultExpire is the number of seconds to cache after the request.
CacheDisable disables caching for relative paths; you wouldn’t want to do this for sensitive areas or areas that would change regular.
Setting Content Expiration
For caching to work correctly, it requires a date that the content has expired otherwise you would need to determine if it was stale every time there was a new request. Using Apache’s mod_expires module, we can set expiration dates for content in whole or individually based on the type or a matching string.
In an htaccess file or the global Apache configuration file, you would end up setting it to be something like this:
As you can see, we can set the default expiration and we can set expiration based on the type.
Caching Without Being Able to configure Apache
In the http headers, we can also set the caching to use if we wanted to be setting the “Cache-Control” header to be something like “Cache-Control: private, max-age=3600”
In html, we can also use a meta tag if we wanted to which would look something like this:
<meta http-equiv="Cache-control" content="private,max-age:3600">
Wrapping It Up