Skip to content

Instantly share code, notes, and snippets.

@splitinfinities
Last active January 23, 2018 06:08
Show Gist options
  • Save splitinfinities/cc9f922275dbe77f41a8b9f10182ee9c to your computer and use it in GitHub Desktop.
Save splitinfinities/cc9f922275dbe77f41a8b9f10182ee9c to your computer and use it in GitHub Desktop.
Caching heavy processing for 24 hours in WordPress
<?php
/**
* This code helps you cache a chunk of heavy processing code in WordPress.
*
* Examples:
* - WooCommerce has quite a lot of these in their plugins. I've used
* this general concept on WooCommerce sites many times, but when using
* it you must be strategic.
* - Membership plugins or anything behind authentication busts the caching layer,
* because the content needs to be pulled from the server.
*
* The idea is to let the processing happen once, grab the result of the processed php, then cache
* it for some time (12 hours in this example) so all visitors in the 12 hour timeframe are only
* waiting on the HTML instead of the processing of MySQL and PHP, then the resulting HTML.
*
* This could introduce side affects if any plugins queue up assets inside of its first function call.
* (that is their fault though, and is very, very rare)
*
* Do not cache anything that is:
* 1. Updated based for a particular user (like Carts, or anything that may have "Added to Cart" in the page. )
* 2. Updated based on quantity (If you have a "quantity remaining" count on a product)
* 3. Updated for different users. (This includes Advertisements or some types of Analytics)
*
* Uses these functions, all available on Flywheel:
*
* Buffers:
* `ob_start`, Docs: http://php.net/manual/en/function.ob-start.php
* `ob_get_clean`, Docs: http://php.net/manual/en/function.ob-get-clean.php
*
* WordPress Transients API:
* `get_transient`, Docs: https://codex.wordpress.org/Function_Reference/get_transient
* `set_transient`, Docs: https://codex.wordpress.org/Function_Reference/set_transient
*
*/
$content_transient = false;
$content_transient = get_transient("key_for_this_content"); // You may need to anme this the slug of the current page, and the section of the page you are caching.
// Forces you to use live content if WP_DEBUG is on.
if (!$content_transient || WP_DEBUG) {
// Opens a buffer to capture HTML.
ob_start();
// This is the block of stuff that has heavy processing. Ideally it's not dynamic, in that it changes every page load.
// Here's a small example
foreach (array_fill(0, 100, 'banana') as $key => $item):
echo "<p>" . $item . "</p>";
endforeach;
// Puts the result of everything above this line into $content;
$content = ob_get_clean();
// This will cache this generated HTML for 12 hours. Whenever someone asks for this HTML, it will pull directly from MySQL or redis.
set_transient( "key_for_this_content", $content, 12 * HOUR_IN_SECONDS);
else {
// set the cached data in the database
$content = $content_transient;
}
// Simply echo the HTML.
echo $content;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment