Modifying Cache Times in Batcache

By default, Batcache, our page caching mechanism, will cache pages for five minutes, if the pages get at least two visits in two minutes. This might be OK for a site that gets a lot of traffic, but could be an issue for a site that does not get a lot of traffic. If this is the case, then modifying cache times may be on your priority list. How can your site take advantage of page caching if it is not highly trafficked and is mostly static?

The answer is to modify the variable settings of the Batcache class.

Let’s imagine that your Pressable site is static. You have a homepage about your company, as well as a few pages about your company and contact information. Furthermore, you want these pages to load from cache, despite your site not receiving a lot of traffic.

In this case, you could insert the following code, ideally BEFORE the /* That's all, stop editing! Happy blogging. */  line. This code will also work if customizing things such as ‘noskip_cookies’:

//Batcache Customizations
global $batcache;

//Check if batcache params are in an object or an array, apply customizations accordingly
if ( is_object( $batcache ) ) {
    $batcache->max_age = 86400; // Seconds the cached render of a page will be stored
    $batcache->seconds = 0; // Time number of visitors required to cache, 0 = instant
    $batcache->times = 1; // Number of visitors required to cache
} elseif ( is_array( $batcache ) ) {
    $batcache['max_age'] = 86400; // Seconds the cached render of a page will be stored
    $batcache['seconds'] = 0; // Time number of visitors required to cache, 0 = instant
    $batcache['times'] = 1; // Number of visitors required to cache
}
// End Batcache Customizations

First, the above makes sure Batcache is being used.

Secondly, we change the $batcache class variables for max_age and seconds from 300 and 120 seconds to 86400 and 0 (instant), respectively. In the case of max_age, this tells Batcache to hold onto a page for 24 hours, rather than the default 300 seconds. For seconds, it tells Batcache to cache the page if the page gets at least two page views instantly, rather than two minutes.*

times in this snippet example is also set to 1, rather than the default 2 visit required to cache.

Finally, after adding the code modification above – feel free to adjust the times to your preferences – your site should load faster and more efficiently for more of your site’s visitors. You can also easily extend cache from The Pressable Cache Management plugin under the Object Cache tab > Extend Cache.

To learn more about Batcache, please see how Batcache works on Pressable.

*The data is stored in memory via Memcache. As the data is stored in memory, this means that it can be removed at any time. Therefore, it is possible that the data would need to be restored in memory, despite the expiration time you have set. See this documentation for further reading.