Caching BuddyPress Using W3 Total Cache

You can get noticeable performance improvements by enabling Object Caching, which will improve page load times for not only BuddyPress, but many other plugins, themes and WordPress core itself.

Caching Methods Explained

Page Caching

When people think of caching, “page caching” is usually what comes to mind first. Unfortunately page caching is not a practical solution if you have BuddyPress enabled. With page caching, when a page is requested a fully assembled HTML file is generated and then served to subsequent users who visit that page (until the cache expires), dramatically reducing hits to the database. Due to the dynamic nature of BuddyPress, page caching would cause content to go stale, defeating one of the core purposes of BuddyPress – live interaction between users. You could theoretically cache pages for logged out users only. But once they are logged in your BuddyPress content would go stale and dynamic elements within the Toolbar, like Notifications, would stop updating until the page’s cache expired.

Object Caching (recommended)

Even though we are not able to cache finished pages, we can still cache a lot of the data that is used to assemble those pages via “Object Caching”. BuddyPress is built to be able to take advantage of Object Caching when you have a persistent caching plugin enabled, such as W3 Total Cache.

WordPress has an API (a set of functions) for Object Caching that allows developers to cache data used by their code. By default, WordPress allows for a form of Object Caching but its lifetime is only for a single page load. If you have a persistent caching plugin installed, you can make the object cache persist longer than a single page load which will reduce the amount of requests to your server and improve page load speed. This is what we’ll be setting up in the next section.

How to enable Object Caching

APC – (private server) (recommended)

If you are hosting your site using our recommended server setup, you can simply contact your web host and request that they install APC on your server (it’s free) and then it will be available to you as a very fast method for Object Caching. Generally you cannot install APC on a shared hosting environment. APC stores the cached values in RAM, which is why I recommend at least 2GB of RAM for ideal server performance.

Once your web host has confirmed that APC is installed and working, you will then need to enable it for WordPress with a plugin. My recommended method is with the APC Object Cache Backend plugin. This is not a true plugin, but rather a “drop-in” file. As such, you do not actually activate the plugin. You just need to copy the object-cache.php file into your/wp-content/ directory. You don’t even need to upload the actual plugin. That’s it! As long as you have APC installed on your server and have placed the file correctly, APC-powered Object Caching should now be enabled and you should begin seeing performance improvements. To confirm that it’s working, you can type this into your browser while logged in as an admin: http://www.yourdomain.com/wp-admin/plugins.php?plugin_status=dropins . It should show APC Object Cache as a drop-in.

tutorials-dropin

You can also enable APC caching using the W3 Total Cache plugin, at Performance > General Settings, under “Object Cache”. If you’re already using W3 Total Cache this may be the preferred method. If you’re not using W3 Total Cache already then I recommend the above plugin as it only requires one file to work!

File caching (shared hosting)

If you are using a shared hosting plan, APC will not be an option and you will likely be limited to file caching, where the cached values are stored in files on your web server. This will ideally result in performance gains, but there is a chance that it could actually result in a negative performance impact if your hosting environment cannot handle it. This is another reason as to why I recommend getting a good server setup and then implementing a proper caching method like APC. But if you are limited to shared hosting, you can try file caching and see if it works for you.

The best method is to install W3 Total Cache. After installing and activating the plugin, go to Performance > General Settings, and then navigate to “Object Cache”. Check the box to “Enable” the object cache. Under “Object Cache Method” it will probably only offer “Disk” as you are on shared hosting. You will see grayed out options for APC, eAccelerator, and other various options that could theoretically be installed on a private server setup. Click “Save all settings” and you have now enabled Object Caching, with the cache saving files to your disk.

tutorials-w3object

After enabling the Object Cache, you can navigate over to Performance > Object Cache to configure the cache options as necessary.

Powered by Zendesk