How to prevent Batcache page caching on Pressable
Pressable uses Batcache and Memcached to store and serve rendered pages. However, there are times where you might not want something cached, such as a specific page or cookie. This guide will help you prevent caching of those items from Batcache.
Before attempting to prevent caching, you will want to make sure that the page is being served from Batcache. You can do this by checking the page headers in your browser’s development tools. If you see x-nananana: Batcache
in the response headers, then you’ll know that the page is being served from Batcache.
Alternatively, you may load the source code of your page. For example: view-source:https://pressable.com/
. You should see an HTML comment like this at the very bottom of the page:
Note that this article will only help resolve issues regarding page rendering. If you are looking to exempt database queries from your cache, you will need to manage those programmatically with object-cache.php functions in your theme or plugin code.
Preventing Caching on Specific Pages
Let’s imagine we want to exempt the following page from Batcache: https://testsite.com/nobatcache/
At the end of your wp-config.php
file, you would want to add the lines of code below:
if ($_SERVER['REQUEST_URI'] == '/nobatcache/' && $batcache) { batcache_cancel(); }
This code will cancel Batcache from being used if the URI matches “/nobatcache/”. Note that if the page is already cached by your browser, you will need to clear your browser cache before the code modification above takes effect.
If you’d like to exclude multiple pages, you can use this code below:
$toExclude = array('/url-1-here/', '/url-2-here/'); //Populate with URLs to exclude. foreach ($toExclude as $exclusion) { if($_SERVER['REQUEST_URI'] == $exclusion && is_object($batcache)) { batcache_cancel(); } }
Excluding page from cache can also be done from The Pressable Cache Management plugin under the Object Cache tab > Exclude Page from Batcache.
Preventing Caching on a Specific IP Address
You also might want to exempt Batcache on a certain IP address: 123.456.789.10
At the end of your wp-config.php
file, you could add the lines of code below:
if ($_SERVER['REMOTE_ADDR'] == '123.456.789.10' && $batcache) { batcache_cancel(); }
This code will cancel Batcache from being used if the URI matches “/nobatcache/”. Note that if the page is already cached by your browser, you will need to clear your browser cache before the code modification above takes effect.
Preventing Caching Cookies
To exempt a user from seeing a cached version of any page on your site, you only need to set a cookie and ensure that the cookie name prefix begins with with ‘wp’ or ‘wordpress’, and Batcache will skip all subsequent pages loaded when the cookie is present.
Heads up!
If you notice your pages are not being served via Batcache, you might want to double check if some active plugins are generating any ‘wp’ or ‘wordpress’ prefixed cookies.