How to Enable the Default WordPress Object Cache
Recently, while attempting to optimize site performance, I found myself experimenting with various caching mechanisms currently available for WordPress. Specifically, I explored each of the following caching options:
- WP-Cache 2 [WordPress plugin]
- WP Super Cache [WordPress plugin]
- WordPress Object Cache [built-in caching mechanism]
While working with the two plugins, WP Cache 2 and Super Cache, I was pleased to discover crystal-clear instructions on each their respective sites. Having access to installation and usage information greatly facilitated the implementation of each of these caching techniques.
On the other hand, finding information about the default WordPress object cache proved virtually impossible. Finally, after locating some decent information, I was able to confirm my initial suspicions and subsequently decided to post a quick article outlining and describing this very straightforward caching method. Although enabling the WordPress cache turns out to be drop-dead easy, it is always good to be sure that you aren’t forgetting a step or otherwise overlooking some important aspect of the process.
Enable default WordPress cache in 3 easy steps
Ready? Implementing the default WordPress object cache is remarkably simple..
Step 1: Enable the caching mechanism
Open wp-config.php
and set the value of ENABLE_CACHE
to “TRUE
”:
define('ENABLE_CACHE', TRUE);
Step 2: Create the cache directory
Create a folder called “cache
” and place it within the wp-content
directory:
/wp-content/cache/
Step 3: Make the cache folder writable
Ensure that the folder is writable by setting its permissions to 755
or 777
.
Done!
Testing
Once you have everything setup, surf around your site and check things out by reloading a few different pages. If the WordPress default object cache is working correctly, you will see a newly added slew of temporary cache files along with a newly created index.php
file and wp_object_cache.lock
file within the cache directory. Thus, if you don’t see any such files in the cache directory after surfing around your site, go back and recheck that you have correctly executed the proper steps. Note: if nothing seems to be happening with your cache — i.e., no cache files are being generated — you may want to try “rebooting the cache” via the following procedure:
- Disable the cache in
wp-config.php
- Delete the cache directory completely from your server1
- Create a new cache directory, set the permissions, and re-enable caching in
wp-config.php
I have also had luck simply deleting the cache directory and letting WordPress recreate it automatically. Remember, if it doesn’t work the first few times, try a few more times before giving up — it does work!
Winning
After you have confirmed that the object cache is working, you’re done. From that point on, or until you disable it, the cache should work as intended, saving you bandwidth resources and saving your visitors time. To verify this, navigate to a long-lost post that is buried way back in your archives somewhere — something that is completely off the radar. As you visit, note the page loading time. Now, visit the page again and compare the results. On average, while the native object cache is nowhere near as effective as either plugin method, it does manage to shave off a noticeable amount of loading time for your visitors.
Wrapping
Although the WordPress object cache may not work as well as either of the cache plugins currently available, it is an effective caching method that is a breeze to setup and run. And, best of all, the default caching mechanism works perfectly with virtually all WordPress plugins.
Footnotes
- 1 Note: One final note concerning the WordPress object cache: all of the files may be safely deleted at your discretion — everything is regenerated automagically ;)
32 responses to “How to Enable the Default WordPress Object Cache”
You don’t ever have to set file permissions to 777 for web applications. Set permissions to the least required, and make sure the PHP user has the required permissions. For example, if PHP is running as apache (mod_php takes on Apache user) or www-data then set the file’s user or group to www-data and give the user or group read and write permissions.
If you do not have the permission to change the user of the file (chown command) then ask your host to do it for you.
According to the Codex, it changed to
define('WP_CACHE', TRUE);
not
define('ENABLE_CACHE', TRUE);
but either way, it’s not working for me yet.
WordPress 2.9 seems to be addressing my desires, at least in some scenarios, with the new Transients API:
http://codex.wordpress.org/Transients_API
Hmm, then how to enable this for 2.9.x?
@Ferodynamics: as mentioned in comment #11, #18, and others, the default WordPress Object Cache was discontinued in version 2.5, so for anything newer you will need to use an alternate caching method.
@cyberhobo: looks sweet – thanks for the tip :)
@bucabay: technically correct, but in practice
777
is often the only setting that seems to work. As sad as it is, many users just don’t care enough or have enough time to be fussing with server settings, help-request tickets, and all of that jazz when all they want to do is “just install a plugin,” if you know what I mean. There are even some major plugins listed in the WP Plugin Repository that explicitly instruct users to use777
during the installation process. Incredible!Thank you for this post
@jeff Exactly. It is the only setting that seems to work, because you’re opening it to everyone. When in fact it is not the permissions that is the problem, it is the ownership of the files. Developers do not realize that they should tell users to chown the files, not chmod them. Those are both meaningless words to users at first, but at least one of them is correct. The chown command. Only if you cannot chown, or if your host is absolutely horrid and will not do it for you, should you resort to opening the files for everyone to write to and execute. (I’d recommend changing servers in that case).
Yep, my entire hosting account was disabled the other day because I had breached the server CPU usage limit.
I was completely oblivious to the fact that you could be penalised for such a thing, but it was recommended that I take steps to improve the caching on my site.
And now I have tried your method, everything seems to be working well so far. Now to see if it has really imroved the situation server-side!