Caching WordPress with PHPFastCache
This project is being rewritten from scratch right now. Want to contribute? Contact me.
In the summer of 2016, the company I work for had a WordPress site that was hitting a new record exceeding one million pageviews per month. In a time where many newspapers were losing steam, we were gaining it.
Our site has a paywall that allows subscribers to view content. We also have a budget. Anyone who has done any monitoring of a WordPress site knows that a big page can pull in hundreds of queries. Those queries start taking a toll on your page load speed as they drain your server resources.
Our first step was to try to minimize queries without causing an issue with forward compatibility on upgrading plugins. Step two was moving to a better host who fit our growing needs, Amazon AWS.
Once our queries were reduced and our servers were setup, we wanted to take a huge step in improving our user experience. We needed to cache our pages differently, users with different memberships needed to be cached differently. That way a guest reader could see one thing and the various tiers of memberships could see others.
There are plenty of great caching plugins out there, but nothing was exactly what we needed. That means I needed to develop it. I have had a lot of experience with caching systems through out the years, but to keep my other projects on track, needed a library. That’s why I went with PHPFastCache by Geolim4 and Khoa Bui. I’ve used in the past in a few projects and have had success. It provides native support for Redis , APC, XCache, MemCache, MemCached, WinCache, Cookie, Redis, Predis, SSDB, MongoDB, and File based caching.
Although it’s not possible for me to provide access to the exact plugin I built due to proprietary reasons, I can share with you a Git that will get you rolling on how you could create your own caching system. It is object oriented and written for PHP7, but will support lower versions.
The code is available on GitHub. Feel free to submit a pull or fork request.
How to make this code your own:
- Download it!
- advanced-cache.php must reside in your wp-content directory.
- The cache directory in your plugin.php file must be writable.
- You must enable caching in your wp-config.php file via define( ‘WP_CACHE’, true );
- Play with the method advancedCache in plugin.php. It is the core handler and is a great place to start learning.
Remember, this is a developmental git that is put together for learning purposes. Don’t use it in a live environment until you have tested it out.
Do you have any comments or suggestions? Feel free to fork, pull or comment below.