What Is Object Cache in WordPress: How It Works, Different Types, and How to Enable It
If you are looking for website optimization options, consider utilizing an object cache.
A WordPress website consists of many PHP objects that are constantly built, initiated, and removed by the memory manager. However, these actions may overload your server, causing your site to slow down and experience errors.
An unresponsive website will result in a poor user experience and turn visitors away, causing financial loss. To prevent this issue, make sure to have object caching enabled on your WordPress site, as it can optimize your server and boost the website’s responsiveness.
In this article, you’ll learn what an object cache is, its advantages, and how it can benefit your WordPress website. We’ll also share the best persistent caching solutions on the market – Redis and Memcached.
What Is an Object Cache?
Object caching involves storing database query outcomes, allowing for future requests to be fulfilled from the cache, avoiding constant database queries. Given WordPress’s reliance on databases as a content management system, this process is particularly crucial.
How Does an Object Cache Work
An object cache reduces repeated and unnecessary queries as it stores the query results from the first request. Let’s see how it works in four steps:
- When a user accesses your website, the browser sends an HTTP request to the web server.
- Once the server receives and approves the request, it delivers the data or resources to the browser.
- If you enable object caching on your site, the server will create a copy of the user requests and store them in your cache.
- Whenever the browser sends the same request, the server will deliver the data from the cache instead of querying the database.
However, if a copy doesn’t exist, the server will generate a new result and create a copy for future use before sending it back to the user’s browser.
Learn More About Caching
What Is Website Caching
How to Clear Browser Cache
How to Reduce Cache Misses
Object Cache Advantages
In this section, we’ll overview the two most important reasons for using object caching:
- Access data in microseconds. An object cache speeds up the data retrieval process since the cached content is available on the server. Subsequently, it improves PHP execution times and optimizes the website’s performance.
- Reduce server workload. Object caching minimizes the number of database queries. It can handle many server tasks, such as delivering the cached content, thus reducing the load, especially during high-traffic times. Furthermore, an object cache can assign cached requests to multiple servers or networks, improving accessibility and ensuring the server works efficiently.
WordPress Object Cache
The WordPress CMS has a built-in object caching mechanism called WP_Object_Cache that automatically stores new objects and primitives from the database to the PHP memory.
A WordPress object cache makes the cached content accessible and retrievable using a key, a unique identifier to indicate a specific entry in the database.
This tool is non-persistent, caching objects for a single page load. Once the request is complete, the WordPress object caching mechanism will remove the cached content. When a browser sends a similar request, the system must cache the object again.
A WordPress object cache is enabled by default to ensure your site runs efficiently. Here are the most common wp_cache functions:
- wp_cache_set – adds new data to the cache by overwriting the existing data. If the key doesn’t exist, the function will create a data set.
- wp_cache_add – if the key exists, this function won’t add the data and will return false. Otherwise, it will add the data to the cache.
- wp_cache_get – if the cache exists, it will return its value. If it doesn’t, the function will return false.
- wp_cache_flush – deletes all cached content.
- wp_cache_flush_group – clears all cached content in a group if the object caching settings support it. If it doesn’t, the function will remove all cached data.
- wp_cache_replace – if the cached object exists, it will substitute the content. Otherwise, it will return false. This function is similar to wp_cache_set(), except it doesn’t add the cached object if the cache exists.
- wp_cache_delete – removes data from a particular cache key.
- wp_cache_add_non-persistent_groups – instructs the object caching mechanism not to copy the group or list of groups in persistent storage. This function is especially beneficial when caching content that should be available only for a one-page load. Note that only several caching plugins apply wp_cache_add_non-persistent_groups.
If you have a small and low-traffic website, the WordPress object caching feature may be enough to manage your website’s cache.
However, large WordPress sites need persistent solutions that will cache objects across multiple page loads. These tools can improve your website’s performance by speeding up the database query transfer while reducing your server’s workload.
On top of that, the persistent caching mechanism can increase traffic capacity, providing a better user experience and helping you scale your business.
For this reason, our higher-tier managed WordPress hosting plans, starting at RM13.99/month, include the object cache feature. We use the LiteSpeed Memcache (LSMCD) system for object caching that you can easily manage on hPanel by navigating to Websites → Manage.
If your hosting provider doesn’t offer a built-in WordPress object cache solution, continue reading as we explain how to install one manually.
Learn More Tips to Improve Your WordPress Site’s Performance
How to Clear Cache in WordPress
How to Speed Up a WordPress Website
Optimizing WordPress With the LiteSpeed Cache Plugin
Persistent Object Caching Tools
We’ll compare the two most popular WordPress object caching tools on the market – Redis and Memcached.
Redis
Remote Dictionary Server (Redis) is an open-source in-memory caching solution that works with various data structures, such as strings, sets, streams, hashes, and lists. Its main goal is to reduce the load on your MySQL database.
Redis offers on-disk persistence, storing data in durable storage like solid-state disk (SSD) to prevent data loss. Depending on your database size, this persistent object caching mechanism provides four persistence options – Redis Database (RDB), Append Only File (AOF), no persistence, and RDB + AOF.
Furthermore, it comes with built-in replication that works asynchronously. This means the process is non-blocking, as the master will continue running queries while the replicas proceed with partial resynchronization or initial synchronization.
With Redis, you can modify an object’s persistence by allocating time to remove and reload new data or enter which new updates to apply when you restart this tool. You can also disable the feature if one or more web pages contain dynamic content.
With these advanced features, Redis can handle sub-millisecond response times, dealing with millions of requests per second for real-time websites.
To enable object caching via Redis on your site, install the Redis Object Cache plugin and open the Settings tab on your WordPress dashboard. Go to Redis → Enable Object Cache.
This object caching tool also offers Redis Enterprise, the premium solution with additional functionality and benefits, such as Role-Based Access Control (RBAC). To purchase the service, you’ll need to fill out a form, and the Redis Enterprise team will follow up with a quote.
Pro Tip
When purchasing a managed WordPress hosting plan, don’t forget to check whether the web host company provides a Redis server.
Memcached
Memcached is an open-source distributed memory object caching system that optimizes dynamic content on various web applications by reducing the database load. It stores data according to key values for objects collected from API calls, page rendering, or database cache and manages them in dynamic memory or RAM.
Compared to Redis, Memcached is more beginner-friendly and straightforward. This object caching tool can store any data size, including a whole HTML page. Memcached allocates each object with a key, raw data, and expiration time. It can work in a standalone server or multiple servers and can be accessed via the Transmission Control Protocol (TCP).
When a browser requests data, Memcached will check if there are any similar objects cached. If yes, it will serve the saved content and send it back to the browser. If not, this tool will return a status code indicating the failure. Subsequently, the server will query the database and send it to Memcached.
Memcached will automatically update the object cache and load the latest data when you update or remove content. If your server runs out of space, the older data will be overwritten. However, if you restart the server, the cached objects are removed. Until Memcached rebuilds the cache, your server may experience an increased load.
As this object caching tool doesn’t have a specific WordPress plugin, you will have to install a compatible extension like WP Super Cache or W3 Total Cache to enable it on your site.
Important! Before downloading the plugin, you must install Memcached on your server or activate it via the PHP Options menu. With the latter option, ensure your hosting provider supports the extension.
Conclusion
Object caching involves storing database queries on the server. This way, when a browser sends a similar request, the server delivers the cached objects instead of fetching the content from scratch.
Object cache can optimize a website’s performance, improving its user experience. On top of that, enabling it can decrease your server load by reducing repeated queries, thus increasing the response time.
The built-in WordPress object caching feature is enabled on your website by default. However, it only caches content on a single page load and is suitable for a small-scale website. If you run a high-traffic site, it’s best to use a persistent object caching tool like Redis or Memcached, as they can work on multiple page loads.
Before installing a tool, check with your hosting provider if they don’t have a built-in object cache feature and support your chosen one. If you have any questions or suggestions, please leave them in the comment section below.
What Is Object Cache FAQ
To help you understand more about an object cache, we will answer some common questions about it.
Can I Use Object Caching With Other Caching Methods?
Yes, but we recommend having one caching method for your site to ensure only one tool caches the database. If you need to enable more caching tools, ensure the configurations complement one another to prevent software conflicts. Furthermore, avoid running redundant or opposite actions, as it can lead to the plugins not working properly.
What’s the Difference Between an Object Cache and a Page Cache?
Page caching is a client-side caching system that stores the entire rendered page, including HTML and content, in the server’s hard disk memory. On the other hand, the object cache is a server-side caching method that keeps database query results on the server.
Why Use an Object Cache Over an HTTP Cache?
Suppose you want to secure your website by implementing user authentication as a security layer. In that case, an object cache is a safer option as it stores customers’ data on the server level. In comparison, an HTTP cache, especially the shared cache, isn’t as secure and may make your customers prone to cyber threats like brute force attacks.
Is a Caching Plugin Necessary, or Is the WordPress Cache Enough?
This depends on your website size. The WordPress cache is better if you own a small site with low traffic. On the other hand, if your website is resource-heavy and has many visitors, we recommend adding an object caching plugin, as it can help optimize your server and boost its performance.