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:

Source code showing page caching through Batcache
Batcache will insert an HTML comment when it caches a page, as well as when it is serving a cached 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/

In this case, we recommend adding a plugin or mu-plugin named pressable-batcache-cancel.php with the following code. 

<?php
/*
Plugin Name: Pressable.com Batcache Cancel
Plugin URI: https://pressable.com
Description: Disables Batcache on specific pages.
Author: Pressable.com, Joshua Goode
Version: 1.0.0
Author URI: https://pressable.com/
License: GPL2

*/
  
add_action('init','cancel_the_cache');
function cancel_the_cache() {
   $uri = strtok( $_SERVER["REQUEST_URI"], '?' );
   if ( in_array( $uri, [ '/nobatcache/' ] ) && function_exists( 'batcache_cancel' ) ) {
       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 expand the array to include the additional page permalinks/slugs

add_action('init','cancel_the_cache');
function cancel_the_cache() {
   $uri = strtok( $_SERVER["REQUEST_URI"], '?' );
   if ( in_array( $uri, [ '/nobatcache/', '/additional-page/', '/another-page/' ] ) && function_exists( 'batcache_cancel' ) ) {
       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 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.