A slow-loading website is not preferred by anyone. If your site takes more than 2 seconds to load, people leave your site immediately.
According to Google, your website should load in two seconds or less. Any longer and visitors start to lose interest.
When it comes to your site performance enhancement, WordPress object caching can help you the most. By configuring object caching properly on your site, you can make your site super responsive to your visitors.
Object caching involves storing database queries and, when enabled on your WordPress site, it can help speed up PHP execution times, reduce the load on your database, and deliver content to your visitors faster.
If you are a new WordPress user and don't know much about WordPress object caching, there is nothing to worry about. Because today, in this blog, we are going to discuss everything about object caching. So, keep reading to know more about it.
How Does Caching Work in WordPress?
Before jumping into WordPress object caching, let's take a quick look at how cache works in WordPress.
When a visitor comes to your website, basically this happens:
- A user from anywhere in the world visits your website and their browser contacts your web server.
- Your WordPress installation contacts the database where it’s installed to fetch your posts and other stored data.
- The web server then compiles this data into an HTML page and returns it to the user.
- If you have caching enabled on your site, the server will create a copy of this request in your cache.
- When a similar request is made again, it will be served from the cache rather than the database.
That means WordPress cache saves the static pages of your site to show it to the visitor without generating dynamic content each time a visitor requests for any specific page or post.
This can improve your site performance significantly, especially, when you're dealing with loads of traffic on your site at a time.
What is WordPress Object Caching?
There are two main types of caching: client-side caching and server-side caching.
There are many types of client-side caching, but the one you’re probably most familiar with is browser caching. This is where the browser stores static web page content so the next time someone visits your site, the page is pulled from the cache on their computer instead of being downloaded again.
Object caching is a type of server-side caching. This means that the cache is stored on the server, not on the user’s browser.
With object caching enabled, your server will create a duplicate of every request made in your cache. The next time a similar request is made, the cache is checked first and the request is served from there, instead of querying the database.
If a copy does not exist, a request is made to the database to be processed and compiled. Once it is processed, it is sent back to the browser and a copy is made in the cache for future use.
Therefore, object caching minimizes the number of times the server has to query the database. This setup can reduce the load on your server, especially during peak traffic times.
Explaining “WP_Object_Cache” Function in WordPress
WordPress has a built-in object cache and that is WP_Object_Cache. It's a WordPress class that can automatically store any data from the database in PHP memory. This way, it can prevent repeated and unnecessary queries to the database.
By default, the WordPress object cache is non-persistent, meaning that it only stores data for a single page load. The objects in the cache will be discarded once the request is over.
For this reason, you might want to consider using a persistent caching tool. With this tool, objects will be cached across multiple page loads, further improving performance.
External persistent object caching solutions like Redis and Memcached makes it possible to persist the object cache between requests. This helps speed up the delivery of database queries while further easing the workload of your server.
3 Best Persistent Object Caching Tools for WordPress
There are a few popular WordPress object caching tools available right now. Among them, Redis, Memcached, and APC have mostly used tools. So, let's check them one by one.
1. Redis
Redis is an open-source object caching solution for WordPress sites that can help to reduce the load on your MySQL database. If you're scaling up your site or having increased traffic, it can be a helpful tool for your site.
Redis stores data in memory instead of a disk or SSD. Due to this, it delivers high performance. It offers built-in replication. You can place data closer to the user’s location for the lowest latency.
Redis works with many data structures such as strings, hashes, lists, sets, and more. It comes with several strong features, including built-in replication, scripting, and on-disk persistence. Redis Sentinel and Redis Cluster provide additional features including high availability and automatic partitioning, respectively.
Once Redis is installed on your server, you can connect it to your site by using a Redis-compatible plugin like Redis Object Cache.
2. Memcached
Like Redis, Memcached is also an open-source, distributed memory caching system. It is used to cache databases, API calls, and page rendering. It helps to improve the application speed by reducing the database load.
Memcached assigns each item a key, expiration time, and raw data. When the user requests a piece of content or data, Memcached will first look in the cache to see if it is stored there. If it is, it retrieves the data from there and doesn’t need to check the database.
If the data is not in the cache, Memcached will return a failure code to the calling application. The application is then responsible for obtaining the data from elsewhere and, optionally, re-submitting the fresh data back to Memcached. WordPress functions such as get_option, get_postmeta do uses cached data.
You'll get a number of Memcached-compatible plugins to use on your site. Among them, W3 Total Cache is a very popular plugin to use.
3. APC
APC or Alternative PHP Cache is a free open-source caching plugin for PHP. With APC caching your PHP script executions can run more efficiently, by cutting down on dynamic PHP executions.
APC is different than Redis or Memcached as it focuses only on PHP caching.
By reducing dynamic PHP executions, script executions run efficiently. When a browser makes a page request, the server will parse the code in your PHP script and then generate the resulting HTML code that will display as a page in the user’s browser.
APC is a PECL module that can be loaded into PHP, but because it operates at the server level it can not be run on our shared hosting servers. If you are on a VPS or dedicated server and are comfortable installing PECL modules, then you can go ahead and install APC on your site.
How to Enable Persistent Object Caching in Your WordPress Site
Now we will show you how to install Redis on macOS using Homebrew. Homebrew is the easiest way to install Redis on macOS. You can follow this tutorial to install Redis on Linux and Windows.
1. Prerequisites
First, make sure you have Homebrew installed. From the terminal, run:
$ brew --version
If this command fails, you'll need to follow the Homebrew installation instructions.
2. Installation
From the terminal, run:
brew install redis
This will install Redis on your system.
3. Starting and Stopping Redis in the Foreground
To test your Redis installation, you can run the redis-server
executable from the command line:
redis-server
If successful, you'll see the startup logs for Redis, and Redis will be running in the foreground.
To stop Redis, enter Ctrl-C
.
4. Starting and Stopping Redis Using Launched
As an alternative to running Redis in the foreground, you can also use launchd
to start the process in the background:
brew services start redis
This launches Redis and restarts it at login. You can check the status of a launchd
managed Redis by running the following:
brew services info redis
If the service is running, you'll see output like the following:
redis (homebrew.mxcl.redis)
Running: ✔
Loaded: ✔
User: miranda
PID:
To stop the service, run:
brew services stop redis
5. Connecting to Redis
Once Redis is running, you can test it by running redis-cli
:
redis-cli
This will open the Redis REPL. Try running some commands:
127.0.0.1:6379> lpush demos redis-macOS-demo
OK
127.0.0.1:6379> rpop demos
"redis-macOS-demo"
6. Installing and Activating the Redis Object Caching Plugin
Once you’ve connected to Redis, you can add the Redis Object Cache plugin to your site.
After installing and activating the plugin, navigate to Settings > Redis in your dashboard. Then click the “Enable Object Cache” button to enable object caching on your site.
That's it! You have successfully enabled WordPress object caching on your site.
Bonus: Is Dokan Multivendor Compatible with WordPress Object Caching?
It's one of the questions that Dokan users ask on a regular basis- is Dokan compatible with WordPress Object Caching?
Yes! Dokan is now compatible with WordPress object caching. If you are using Dokan latest version Dokan 3.5.0 or above version, you can enable object caching and use Dokan seamlessly on your site.
So, install a WordPress object caching solution like Redis or Memcached on your server and connect it with your site using a compatible plugin like W3 Total Cache or Redis Object Cache. Then use Dokan multivendor plugin for your online marketplace and get the full benefit of WordPress object caching.
Previous versions of the Dokan multivendor plugin (before Dokan 3.5) did not support object cache. But after releasing Dokan 3.5 version, now it is totally compatible with WordPress object caching.
WordPress Object Caching – FAQ(s)
Caching refers to the process of creating static versions of your content and serving that to visitors. Static pages are generally rendered quickly in browsers. This leads to faster performance of your website.
Transients can last one second or one day, actually depends on how much time you want them to exist. However, it won't be around after the expiration time. That means you can set an expiration time and it will last till the expiration time.
It improves site performance by reducing the load on the database. By enabling caching solution on your site, you will be able to store static versions of your website in a cache. This allows WordPress to skip running heavier PHP scripts every time your site loads.
WordPress does not automatically delete expired transients. Over the span of months and years, these expired-but-not-deleted transients living inside the wp_options table of the database can begin to slow things down and impede site performance. For this reason, we should remove expired transients to improve WordPress site performance.
Yes, WordPress has a built-in object cache called WP_Object_Cache. Introduced in 2005, it provides a way of automatically storing any data from the database in PHP memory to prevent repeated queries.
WordPress Object Caching- A Quick Recap for You
WordPress object caching is crucial if you want to improve your WordPress site performance. It can reduce your server load by not running heavier PHP scripts every time your site loads. Instead, it shows a static version of the requested file from your cache to the visitors.
You can set up and configure object caching on your site by following our tutorial. Also, you can enable object caching on your online marketplace and use Dokan smoothly as now Dokan is completely compatible with WordPress object caching.
So, that's all from our end on object caching. Now it's your turn. Enable object caching on your site and let us know if you face any complications while configuring caching on your site through the comment box below. We would love to address your problem at our earliest convenience.