How to Enable the Default WordPress Object Cache

Published Wednesday, December 26, 2007 @ 2:27 pm • 30 Responses

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:

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.

How to enable the default WordPress cache in 3 easy steps

Note: This information applies to WordPress versions less than 2.5 only.

Ready? Implementing the default WordPress object cache is remarkably simple:

1) Enable the caching mechanism — open your wp-config.php file and set the value of ENABLE_CACHE to “TRUE”:

define('ENABLE_CACHE', TRUE);

2) Create the cache directory — create a folder called “cache” and place it within the wp-content directory:

/wp-content/cache/

3) Make the cache folder writable — ensure that the folder is writable by setting its permissions to 755 or 777.

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:

  1. Disable the cache in wp-config.php
  2. Delete the cache directory completely from your server 1
  3. 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!

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. 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.

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 ;)


Dialogue

30 Responses Jump to comment form

1Rasheed

December 26, 2007 at 9:31 pm

Thank you for this post.

But what about chmod 777 ?

Is it safe ?

Thanks

2DeepFreeze

December 27, 2007 at 3:10 am

The point that you mentioned, “saving you bandwidth”, I am quite sure that it might still be the same. But I am definitely sure that the Web Server CPU usage will drop down dramatically.

3Ozh

December 27, 2007 at 9:07 am

Indeed, it won’t save any bandwidth.

A noteworthy aspect of caching (be it WP-Cache, the built-in cache or whatever): whenever your blog takes ages to load and you have no clue where the problem comes from, just delete the /cache folder. More often than you’d think, it will fix everything.
On Dreamhost, I simply deactivated the cache on all the blogs I run because it was locking too often (I think the efficiency of it depends on some NFS configuration)

4Perishable

December 29, 2007 at 1:07 pm

@Rasheed: If you are able, you should definitely use 755 over 777. 755 grants fewer permissions and is thus the preferable choice, however, this setting does not always work under all circumstances and on all servers, whereas 777 — although not ideal — generally does the trick.

@DeepFreeze: Yes, I see that now, I honestly don’t know what I was thinking (if anything) when I wrote that. I suppose it made sense at the time.. I guess that’s what I get for self-editing my content. I will definitely be fixing this — thanks for pointing it out ;)

@Ozh: Ouch! It hurts now ;) Sorry for the misinformation! Also, thank you for mentioning the “cure-all” cache-deletion trick. I have experienced this myself on more than one occasion, including, oddly enough, setting up the WP cache itself. It is a strange, frequently useless beast, I concur, however, it may prove beneficial for certain individuals and/or specific situations. Hopefully, this post will help shed some light on the overall implementation process.

5DeepFreeze

December 30, 2007 at 9:22 am

;-), Anytime Bro Anytime

6Frank

January 7, 2008 at 2:37 am

More Options for WP-Cache:

define('ENABLE_CACHE', true); // Cache on
define('CACHE_EXPIRATION_TIME', 604800); // Cachetime in seconds

and a small Plugin for control the cache:
http://bueltge.de/wordpress-cache-kontrollieren/479/

You can clear the cache with one click.
With best regards
Frank

7Perishable

January 8, 2008 at 2:13 pm

Frank,

Thanks for reminding us about the CACHE_EXPIRATION_TIME option.. I was going to mention it in the article, but apparently failed to do so, for some reason or other..

Also thanks for sharing the WordPress Cache Controller plugin with us — I am sure users will find it useful! Clearing the cache with a single click is definitely sweet ;)

Cheers,
Jeff

8Frank

January 8, 2008 at 11:50 pm

Welcome!

I have update this plugin with small addon. The plugin clear the cache, when you puplish, edit or delete comment and when you cjange private to puplish. You must not click, this functions is automaticly.
With best regards and sorry for my bad english.
Frank

9Perishable

January 9, 2008 at 11:47 am

Very good, Frank — thank you for sharing the update news with us!

Cheers,
Jeff

10Abhinav Singh

October 15, 2008 at 12:37 pm

For me this never worked.

I tried deleting the cache folder, enable disable the things a few times but I could never see any files being generated in the cache folder.

Sorry, but your post sucked for me :(

11Jeff Starr

October 15, 2008 at 1:28 pm

Hi Abhinav Singh, thanks for taking the time share your experience with us. Your frustration with the WordPress Object Cache is shared by many, including myself, hence the tutorial above. Hopefully you realize that the file-based object cache has been removed from all WordPress versions 2.5 or better. Looking at the site you linked to in your comment, it looks like you are running WordPress 2.5. Perhaps this is the issue?

Regardless of any specific problems you might be having, I have to emphasize that the methods described in my tutorial are peer-reviewed and quite sound. They have worked for a number of people running various versions of WordPress in a variety of different environments.

Btw, you shouldn’t fake your Feedburner subscriber count — it is dishonest, desperate, and makes you look like a real loser :(

12Abhinav Singh

October 15, 2008 at 1:32 pm

Man it worked when I followed the steps and installed WP-Cache-2. So I am not sure where the problem was. Now I can see the cache files in the cache folder

And kuddos to you man. You are the first one who has found that out from over a thousand visitors to my site.

So now probably its time to release this hack where people can put the same on their sites :) Nothing that I am a looser, I created this as a hack. Though to release this for everyone, but then help myself back till someone picks me up on this ;)

Kuddos dude ;)

13btFish

December 9, 2008 at 12:31 am

There is something wrong wordpress2.7

14Jeff Starr

December 9, 2008 at 8:25 am

@btFish: Check out comment number eleven for an explanation.

15cyberhobo

December 14, 2008 at 9:44 am

Thanks for this bit of illumination on a dark topic. It still seems a bit mysterious to me what the object cache does since the removal of file-based caching. Are cached objects no longer persistent across requests? And why are there still freshly dated files the cache directory of my WP 2.7 installation?

Look at the code I would guess the answer to the first question is no, but the second question makes me think I’m missing something.

As a plugin author, I’d like to be able to cache things persistantly without depending on other plugins, but it’s not clear that the object cache provides this ability.

16Frank

December 14, 2008 at 10:55 am

WordPress bigger 2.6 has not a cache-directory, WP has now a object-cache.

17JK

December 23, 2008 at 12:12 pm

Jeff, I too tried several attempts but no success yet on this one.

Do I need to have Wp-Cache2 and Wp-Super-Cache plugins installed to enable this built-in feature?

Thanks, JK

18Jeff Starr

January 1, 2009 at 10:34 am

@cyberhobo, @JK: As mentioned in comment #11, the file-based object cache has been removed from all WordPress versions 2.5 or better.

19cyberhobo

January 3, 2009 at 7:05 am

Thanks for answering. The files in the cache directory that are puzzling me must be created by something else. They are named with a hash ending in .spc.

20jidanni

February 11, 2009 at 6:27 pm

Maybe you should add a warning at the top of this article, that one can no longer (do the title of this article) “Enable the Default WordPress Object Cache”, (which is apparently the case, if one reads through the comments.) And mention if there is any built in alternatives, or must one resort to plugins? Thanks.

21Jeff Starr

February 15, 2009 at 9:11 am

@jidanni: Thanks for the advice. I have included a note to this effect in the article. Cheers!

22php trivandrum

May 11, 2009 at 5:10 pm

On an academic interest resurrected the file based object cache for wordpress. http://www.php-trivandrum.org/wordpress-plugins/wordpress-object-caching-in-file-system.html

23Jeff Starr

May 12, 2009 at 3:44 pm

@php trivandrum: Good stuff! Thanks for sharing! :)

24bucabay

July 6, 2009 at 5:04 am

I’m working on a PHP Object Cache. An implementation for wordpress is coming along:

http://code.google.com/p/php-object-cache/

Subscribe to comments on this post


Share your thoughts..

TopRead official comment policy

← Previous post • Next post →

« December 22nd, 2007Are Adblock-Plus Visitors Seeing Your Content? »

Contact Perishable Press

  • Contact Jeff via form

Search Perishable Press

About Perishable Press

Perishable Press is the virtual playground of Jeff Starr — visionary, founder and lead developer of Monzilla Media, a small web and graphic design company in the lush desert oasis of Moses Lake, Washington. Perishable Press features articles and tutorials on many aspects of digital design..

Read more..

Perishable on Twitter

Twitter's new "xx new tweets" feature is a little too close to an annoying nag for my tastes - always makes me feel like i am running behind

Perishable on Tumblr

Insane October

Sun, 01 Nov 2009

By far the most insane month of 2009, October included the following activities:

1st week: Trip to the East Coast, beginning with some business in Connecticut.

2nd week: East Coast trip continues with much pleasure in downtown Manhattan.

3rd week: Photo and art excursions with good friend visiting from Portland, OR.

4th week: Marathon book-editing and fine-tuning for Digging into WordPress.

Now that November is here, things remain busy, but I am hoping to get a chance to restore some balance and regain my equilibrium. Of course, the holidays are right around the corner..

Import Feeds to Facebook

Mon, 07 Sep 2009

Seems like a lot of misinformation and confusion out there on how to import and display your feeds on Facebook. Here is what worked for me:

1. In the lower left-hand corner of your Facebook account, click on “Applications” > “Notes”.

2. In the upper mid-right column, click on “Import a blog” in the “Notes Settings” panel.

3. In the “Import an External Blog” panel, enter your feed URL and check the little box.

4. Click the “Start importing” button and then click on “Confirm Import” on the preview page.

That’s all there is to it. Don’t forget to edit your “Notes Privacy” settings to ensure that people can see and comment on your imported feed items.

Once you successfully import your feed(s), they will appear by clicking on the “Notes” button in the left sidebar of your Home page. Also, your timeline or “Wall” will also display the most recent post from each of your feeds as they are published and pulled into Facebook. This makes it easy for your “Friends” to see what you have been up to elsewhere on the Web.

help me in plain english

Mon, 31 Aug 2009

This has got to be the most ironic comment I have ever read:

“hi i dun a stupid noooby mistake and dint think about encrytion i just put a pass in the change pass box and now when i attempt to see my main.php or index.php its sayin password no and error how can i reset back to having no password or were can i edit the bit so that a pass is automattically seen or if not posable how can i make it so i can put in the pass i made at some point so i can login this way? the 3rd is most prefered as this will help me with other projects i am planning as i am a php noob :s plz sum1 hu is clever help me in plain english”

Thanks, “jay” — you made my week with that one.

Redirection After Registration

Tue, 04 Aug 2009

After searching high and low for an unobtrusive method of redirecting users to a custom URL after registering at a WordPress-powered site, I finally resorted to (gasp) hacking the core. I simply could not find a better way of doing it that didn’t require a ton of additional code. I found several ways of redirecting users to various URLs after logging in and out, but absolutely nothing seems to exist on redirecting users to, say, the home page, or better yet, back to the current page after registering as a subscriber (or whatever role the Admin has set for new registrations). Indeed, the only way to direct a user to some page other than the default WordPress “Registration complete. Please check your e-mail.” screen (which looks just like the WP Login page, btw) is to hack the wp-login.php file.

Thus, for the sake of remembering this technique, helping others, and/or “inspiring” someone to find a solution, here’s how to hack WordPress to change the page that users are directed to after they register (via submission of a username and email address). First, open the wp-login.php file and find the line that says, “wp_redirect('wp-login.php?checkemail=registered');”. That’s the key right there. To change the location, replace the part that says, “wp-login.php?checkemail=registered” with the URL to which you would like to direct the newly registered users. You may use full URLs or even relative paths to a specific file. That’s all there is to it. It’s still hacking the core, but not by much ;)

Remember, if you’re going to hack the core, make a note of the change(s) and refer to it before/after each subsequent upgrade.

A 500KB wp_options Table is Too Much

Thu, 09 Jul 2009

After my server crashed, I found myself restoring my site’s WordPress database. While there, I decided to dig around a bit and make sure everything was up to snuff. While looking through the wp_options table, I was surprised to discover that WordPress seems to cache around 400KB of “Planet WordPress” dashboard feeds (among other things). That’s a little extreme if you ask me, so I decided to clean things up and reduce my overall database size by around half a megabyte. Here’s how I did it using my archaic 2.3 version of WordPress and phpMyAdmin.

Step One: Take Some Notes

Before doing anything, copy and paste a few text snippets from your dashboard feeds. This will enable you to easily locate the oversize options fields for removal in Step Three. Note: permalinks from the various feed entries make for good search candidates.

Step Two: Kill the Feed

Place this in your theme’s functions.php file (props to Michael Shadle for the code):

function remove_dashboard_feeds() {
remove_action(‘admin_head’, ‘index_js’);
}
add_action(‘admin_head’, ‘remove_dashboard_feeds’, 1);

Step Three: Clean up Your wp_options Table

Once that is done, you may clean up your database by doing a quick search for some of the text snippets (permalinks work great) that you copied from the dashboard feeds in Step One. The field(s) that you find should be named something like “rss_123abc…”, where the “123abc…” is some long, apparently random alphanumeric string. Once you have disabled the feed via functions.php, feel free to reduce the size of your database by deleting the field(s) used to store data for the dashboard feed.

Doing this saved me an extra ~400KB of space, which is much-needed as the size of my Perishable Press database continues to grow.

Read more on Tumblr..

Subscribe to Comments Recent Dialogue

  • Peekay: Xoops users who allow user registration are going to need to comment out: # RedirectMatch 403 register\. from the 'Specific ...
  • Regan Frank: Hi, Just wanted to say thanks for this hack. I really needed something to work for me and your code was a god send. Thanks...
  • Sarah: You guys have some decent SEO. I pulled you right up on a yahoo search. Nice site too!...
  • Peekay: My problem is similar to the one highlighted by Rick Beckman. Our spammer is submitting a GET request for the contact form on my site...
  • kimuffessymet: im too complex to discarded to the gym, anyone advance where i can get a elliptical trainer so i can work at habitation. As this is t...
  • Tom: Just one thing - Windows Mobile 6.5. This seems to be barfing on all my otherwise good work. :(...
  • ILT: Hi there, Fantastic feature! However I need to show the full text description, not just the excerpt. Can I achieve this using th...
  • Nicolas: Thanks for the tip. I had some corners that would ideally have been round, but I had a dynamic change-color script that would have...
  • Nik: you are amazing. thank you for writing this....
  • Jeff Starr: Thanks Kim! I hope you enjoy the book :) Thanks for your support....

Read more recent comments..

Attention: Do NOT follow this link!