What is PHP?
Most of the websites we host use PHP for scripting. PHP is a programming language used to create dynamic contact on a web page, like show a list of data records from a database, retrieve advertising, or show a news feed or Twitter stream. A way to speed up all of that is to use PHP caching.
What is Caching?
Web pages load faster by storing parts or all of the page in memory, in a file on the web server, or with the content distribution network (CDN) instead of creating it each time someone retrieves that URL. That is called caching. That provides for a better user experience as users do not have to wait as long when they click for the next web page to load.
You can cache content, like images, HTML, and data. It’s logical to do so. For example, it’s not necessary to retrieve the same image 10 times for 10 different people. Instead it is better to cache that. The cache stores that web page with all the data already in place, like a list of names.
Another example is a Twitter or news feed. It’s not necessary to consult the same news site or Twitter feed 10 times. Just do it once.
The Programmer’s Cookbook
Consider a database call. It does not make sense to read the database 100 times for 100 people all loading the same data records, such as something that is common to many people, like the price of an item that you are promoting up front. Instead, it is more efficient to fetch that value from cache.
PHP also caches API calls. An API is how one website retrieves content from another. For example, an API is how in-page advertising works. But that is a counter example, as you would not cache an ad since the advertiser shows different ads to different people.
So caching is not applicable to all situations. In particular, it is not suitable for data that changes frequently or is unique to each user.
PHP by itself does not do caching. But there are opensource frameworks to let the programmer design their code to work this way. In other words, it lets the code check if the data has already been retrieved before it goes and fetches it again, which is a relatively slow process since it has to query the database. With the framework, the programmer does not have to worry about the technical details of caching, so it’s easy to use.
PHP Caching and WordPress
WordPress is not programmed to use PHP cache. But you can add the plugins W3 Total Cache or WP Super Cache for that. That will help speed up your content management publishing as well. And any custom WordPress code you write can use caching if you add a framework.
Caching also works with widgets. Widgets are preprogrammed bits of code that do some common function, like verify a CAPTCHA phrase. These widgets are almost always coded in PHP since WordPress is written in PHP.
Tuning the Cache
Now, if you are a thinking person, and we assume you are, as all of our customers are clever people, then you might say “This is no panacea. How can it know what are the last 5 headlines from the news?” without checking that. The answer is it does not.
Caching is controlled by size and time. So you could set the cache time to 5 seconds or 5 minutes.
When the cache time expires the PHP code makes the round trip to the database server or API to check to see if anything is changed.
Also it can be set by size so that really busy sites flush the cache and fill it up again after a certain amount of page hits.
Finally, there is a highly complex type of caching called isomorphic programming that uses caching in the browser to only rewrite the parts of the web page that have changed.
PHP is as ubiquitous as HTML in 2016 and is used in practically all dynamic websites that we browse today. Savvy developers need to leverage caching for increasing PHP performance.
From Varnish to MemCached, to the plethora of out of the box PHP caching plugins available for various CMS, caching is now cemented into the workflow of every self-respecting PHP programmer out there. We hope you enjoyed this brief introduction into the world of PHP Caching, and if you have any questions – comment below.