Plugin Output Cache

Caveat

Unfortunately, due to ill-health, this plugin has not been developed or supported properly for some years. It works with the latest versions of WordPress (including on this website) but could possibly conflict with any WordPress features added after 2008 — e.g. custom post types — if you use them.

Purpose

This plugin provides a way for other plugins to cache their output and speed page display. Most of my plugins use the cache automatically if it is installed. The cache is cleared automatically whenever the blog content changes so that the retrieved output is always fresh.

This is version 4.0.8 download latest version. It is faster, easier to use, and also cleans up after itself when deactivated.

  • version 4.0.8 generalises the utf8 bug fix to earleir versions of MySQL.
  • version 4.0.7 fixes a bug handling utf8 characters.
  • version 4.0.6 fixes a bug with un-escaped content and removes a warning message from the PHP error log.
  • version 4.0.5 Addresses some compatibility issues, adds a little more speed, and fixes a stray link.
  • version 4.0.0 rewritten for ease of use and even better performance. If you have several plugins installed each using the cache they share a single instance. The cache has a two-tier architecture, first caching in memory and only committing to the database on shutdown.
  • version 3.0.1 tweaked the code a little.
  • version 3.0.0 is faster and uses far less memory
  • version 2.1.2 beta allows cache efficiency to be measured
  • version 2.1.1 beta fixes a bug when the database has a prefix other than ‘wp_’

Installation Instructions

  1. If you are upgrading from an earlier version first deactivate the old plugin via the Plugins menu and then delete the plugin directory from your server.
  2. Upload the whole plugin folder to your /wp-content/plugins/ folder.
  3. Go to the Plugins page and activate the plugin.
  4. Go to the Manage page and switch on caching.

Usage and Options

The plugin does nothing on its own! It exists only as a helper for other plugins. However, if my other plugins are installed they will automatically have their output cached.

You can tell if the cache is working properly by looking at the source of a browser page which uses one of my plugins, e.g., Recent Posts. Underneath the output from the plugin there will be an HTML comment telling you how long the plugin took to produce its output and whether it came from cache.

If you upgrade any plugin that uses the output cache plugin remember to clear the cache or you will not see the results of the change. The Plugin Output Cache installs a page under the admin Manage menu with a button to clear the cache and a button to switch caching on and off.

The Manage page also give you the option to collect data on how efficient the cache is on your blog. N.B. Collecting the statistical data makes the cache about 20 times slower so it should only be done to satisfy your curiosity and not as a a matter of routine.

I have defined efficiency as the percentage of cache requests actually served from the cache. After the cache has been cleared the first requests are for items that have not yet been cached and efficiency will be poor but as repeat requests are made the efficiency rises rapidly. In my experiments 85% – 95% are common. Of course every time someone makes a comment the cache is cleared and the efficiency drops again.

How to Use Plugin Output Cache for your Plugins

The Plugin Output Cache can be used by other plugins which produce output by making minor changes to them.

The Plugin Output Cache defines a constant POC_CACHE so you can check if the cache is available. Version 4.0+ also defines POC_CACHE_4 which lets you take advantage of version 4’s new features. Plugins adapted to work with earlier versions will continue to function normally.

The Recent Posts plugin shows in detail how to do it incorporate caching. The outline is as follows. Instead of:


function your_plugin() {
	$result = get_plugin_output();
	echo $result;
}

Use:


function your_plugin() {
	if (defined('POC_CACHE_4')) {;
		// $key is some unique string to identify the output
		$result = poc_cache_fetch($cache_key);	
		if ($result !== false) echo $result;
	}
	$result = get_plugin_output();
	if (defined('POC_CACHE_4')) poc_cache_store($cache_key, $result); 
	echo $result;
}

Note: This presumes your plugin can be written to return its output as a string or signal failure by returning ‘false’.

Your code should also clear the cache whenever your plugin’s options are updated.


	if (defined('POC_CACHE_4')) poc_cache_flush();

112 replies on “Plugin Output Cache”

  1. I think what Ajay means is what the author’s plugin should do if your plugin isn’t installed, but I think you’ve already got that covered.

    I’m gong to try it with my Latest Comments With Avatars plugin which can be slow as it retrieves the avatars on every page load.

    I also have APC and WP-Cache installed on my site, so I’ll give you some good feedback

  2. Tin: If you use WP-Cache you should have less need for this plugin. When WP-Cache has to render new pages rather than fetch them from the cache, this plugin should, in principle, speed that up since many pages uses the same block of output from Recent Posts say. I don’t really think that will have a big impact though and if you run WP-Cache you probably don’t need the Plugin Output Cache. But I’d like to be sure about whether having both installed messes both up or if they play well together.

  3. Hi Rob,

    indeed, that’s a good idea to cache plugin’s outputs, and I’m just trying around with your PlugIn on a WordPress 2.1 installation. But at first it did noch work for me – because I have an other database-prefix in than “wp_” in my “wp-config.php”-definitions.

    For WordPress 2.1 I had to change the code from line 42 – 52:

    // delete all the data in the cache
    function clear_all() {
    	global $wpdb;
    	// $wpdb->query("DELETE FROM `wp_options` WHERE `option_name` LIKE '" . CACHE_PREFIX . "%'");
    	$wpdb->query("DELETE FROM `".$wpdb->prefix."options` WHERE `option_name` LIKE '" . CACHE_PREFIX . "%'");
    }
    	
    // count how many entries are cached
    function count_entries() {
    	global $wpdb; 
    	// return $wpdb->get_var("SELECT COUNT(*) FROM `wp_options` WHERE `option_name` LIKE '" . CACHE_PREFIX . "%'");
    	return $wpdb->get_var("SELECT COUNT(*) FROM `".$wpdb->prefix."options` WHERE `option_name` LIKE '" . CACHE_PREFIX . "%'");
    }

    As I don’t have any WordPress$table_prefix instead of $wpdb->prefix …?

    Anyway – Good work – And I think, your PlugIn will help lots of people getting their blog running much faster.

    Thank you,

    Gunnar

  4. Ooops!

    Some of my words words were “stolen” by “making” HTML-tags of < and the following letters. I wanted to write:

    As I don’t have any WordPress<2.1 running at the moment, I could not try what is to do for earlier versions. If these changes don’t work, it might help to write $table_prefix instead of $wpdb->prefix …?

    Sorry about that.

    🙂

  5. Hi Rob,

    as I have also some problems with WP-Cache, I am now using your PlugIn not only for caching the output of other PlugIns:

    I have copied (and renamed) some wordpress-functions like wp_list_pages and others into an extra PlugIn, so I can use them with POC-cache.

    It works great and the performance has improved extremely.

    Okay, there must be an individual $key for everything that’s cached, and some of the wordpress-functions have to be cached “per post” – so that I produced about 500 of POC-entries in my database, now – but … so what?

    🙂

    I’m trying around – and there’s still a lot of space in my database …

  6. pufaxx: I’m glad to hear it’s helping! My similar posts plugins also produce output per post so I’ve been watching the number of cache entries get well into triple figures but so far at least it doesn’t seem to have any bad effects on the database and the cache-retrieval times aren’t affected. I have toyed with setting up a custom table for the cache rather than using wp-options but the present method works so well I’m am loathe to re-invent a very smoothly running wheel.

  7. lee: WP-Cache caches all the output generated by WordPress so that whole pages are stored. Furthermore, when it is working it automatically does its work.

    The Plugin Output Cache is different. It only does anything if a plugin author has adapted their plugin to use it, to save regenerating its output needlessly. WordPress still generates its pages dynamically but pulls in the cached plugin output where it belongs.

    This plugin shouldn’t interfere with other plugins.

    The Plugin Output Cache isn’t a replacement for WP-Cache as they do different jobs.

    Does this answer your question?

  8. lee: The Plugin Output Cache only caches the output from plugins that have been specially written to use it. In principle it should have any interactions with other plugins… I haven’t tried it specifically with the plugin you mention.

  9. (I’m using POC 2.1.2 beta, as well as Similar Posts 2.1.1 beta)

    Had a strange bug that plagued me for a while before I got around it. I created a new post and saved it. Then I changed some options in the Similar Posts, surfed around my blog, and later changing my mind by setting the options back.

    The problem was that the Similar Posts were cached. I cleared the POC cache, and refreshed the page on the new post. But the old cached data appeared. I turned off the cache and cleared it. New Similar Posts HTML showed up as expected. Turned the cache back on and received the *old* cached HTML again. I hit the clear cache button several times, and it returned 0 items cached each time.

    The eventual solution was to edit *another* post, after which the cache seemed to actually clear properly and the correct HTML was shown. Now all is fine.

    Sorry for the longwinded post, but I’m trying to be as clear as possible. This generally seems to work well enough that I think you should take over the ailing WP-Cache2. 🙂

  10. WordPress already has a caching system built in that is very similar to this – take a look at wp-includes/cache.php. It’s used internally by WordPress and is available to plugins too.

  11. Mark: Unfortunately I have never been able to get the built-in cache to operate on shared hosting running in safe mode. As I understand it the built-in cache is also off by default since it can result in an overall speed decrease. The plugin output cache answers a more specific need.

  12. hello,
    I have a problem with plugin output cache and recent posts plugin (both your plugins). recent posts works great, uses the changes I set in the options page, but if I activate plugin output cache, it shows me (for any posts) only 3 links to recent posts, altjhough I have highly customized the formatting and output in the options page of recent posts plugin.
    Emptying and even disabling cache does not help, I have to deactivate the plugin output cache to get the customized results.

  13. ovidiu: I am mystified. I can’t reproduce the problem here and the plugin cache and recent posts seem to be working together. Can you give me any clues about your setup that might be relevant? We need to get to the bottom of this.

  14. I don’t think I can give you any clues… lots of plugins working on this site.
    All I can offer is an admin account for an hour or so, so you can have a look for yourself. This is all I can try because I am currently working on this site, and I am almost done with it.

    ONE more thing I’d like to try is give this another go from home, as at work (currently sitting there) I have severall proxies and firewalls between me and the net so it might be a proxying issues although I doubt it.

    I’ll contact you again here later on today after work.

  15. Plugin Output Cache version 3.0.0 has been rewritten for greater speed and lower memory consumption. Please give it a try and let me know of any issues.

  16. hello, having some problems on wp 2.2.3
    using recent posts, recent comments and plugin output cache, all latest versions as of now.

    POC shows 2 items in cache – always.

    check it out:

    http://www.ronduldesibiu.ro/ read any article, below every post, it should show 3 recent posts from the same category.

    I call it like this inside my template:
    <?php if(function_exists(‘recent_posts’)) {echo(‘Alte articole din aceasta rubrica:’);recent_posts();} ?>

    3 categories are excluded and all others are included.
    BUT the recent posts shown below are not limited to the same category although I checked this.

    can you have a look please?

  17. ok, I have to deactivate the plugin output cache now, can’t let it work online anymore, as it mixed everything I described above up for me.

  18. ovidiu: I’m sorry you are having these problems. I did look at the site but it gave me no clues. Am I right in understanding that recent-posts works properly if you deactivate the plugin cache?

  19. yes, I deactivated the poc and now all is fine…

    I will live without it until I really need it and then try again maybe with never versions,,,..

  20. I’m using several of your plugins (Recent Comments, Recent Posts etc), all latest versions, on WP 2.3.2. The problem is that the plugin cache doesn’t seem to be working properly. When the cache is disabled, things look fine and I see “POC Cache Miss” comments in the output.

    However, when I enable the cache, the Recent Post/Comments plugin ALWAYS show “None found” in the output, and the comment still says “POC Cache Miss” despite refreshes.

    I have WP Super Cache installed, but I tested with Super Cache disabled and I still see this problem. Any thoughts? Similar to the problems ovidiu seems to be having.

  21. Rob: yep, I had flushed the caches. I think I have identified the problem though. For some reason, the widget was only displaying posts that matched tags of the current post even though it was NOT set in the options. I set and reset that setting again and now it seems to be working fine. Thanks!

  22. Shanx: It didn’t conflict with WP-Cache… I haven’t heard of any conflicts with its successor either and I don’t expect any. I’d be interested in finding out for sure though.

  23. Hi Rob,

    Call me dumb. LOL!

    I have your other plugins Similar Posts and Similar Posts Feed, so i just turn Cache on and that’s it right?

    Great to know it didn’t conflict with SuperCache though.

  24. Fendi: Yes, just activate the plugin and then go to the Manage page and switch it on. You can check it is working by visiting a few pages with Similar Posts showing and then checking the POC Cache page to see that some stuff is stored in the cache.

  25. Took me about an hour to realize that the plugin wasn’t working because I had changed the default folder name from POC_Cache to poc-cache, and the add_actions in the poc-cache-admin.php weren’t being fired off.

    You might want to try using something like the following:

    'activate_'.$this->plugin_basename(__FILE__)

    (I’m hoping the above looks OK first time because you don’t have a Preview Comment button!)

  26. @Rob: Because 99% of all plugins use lowercase for folder and file names, and they generally use a hyphen instead of an underscore, so it just looks better to have them all look alike. I just went ahead and changed it because it didn’t even occur to me that the folder would be hardcoded.

    I used to use your plugin a long time ago (comment #27 above is from me, too!) and for whatever reason I had deleted it. Just picked it up again which is why I reinstalled from scratch and ran into this issue. The latest versions (currently in development and QA) of a few of my existing and new plugins will be POC-ready. My pages are now loading faster by about 0.2 seconds each, and with 10% fewer DB queries. Thanks!

  27. Hi everyone.. I need a little help, and was wondering if any of you would like to take a quick job on.. I’ll be willing to pay a nominal fee if someone can add the POC code to three of my plugins:

    1. Event Calendar – It’s kind of complicated, and performs multiple queries and LEFT JOINS.
    2. Recent Comments (not Rob’s.. a different one, I had issues with styling, etc.)
    3. Show top Commentators

    Additionally, if you’re talented, if my Event Calendar plugin could be reconfigured to query the database only once per day (say at 5:30am daily), otherwise stored and cached output would be displayed. I use this plugin only to simply display a short list of upcoming events in my sidebar.. with URL to post..

    I’ve zipped and uploaded the three plugins in question.

    Any takers? I desperately could use the help. My site has grown so fast and I cannot seem to “crack the code”… this on top of all the MySQL and Apache tweaking I’ve needed to do.

    Your help would be greatly appreciated.

    You can email me at periginald@gmail.com
    Thanks.

  28. Despite my long history with this plugin, I have been completely unsuccessful getting either v3.0.1 or v4.0.0 to work with WP 2.5.1. I do not use the standard “wp-” table prefix, but the v3.x plugin worked fine with WP 2.3.3 and my nonstandard prefix. so I doubt that’s the issue.

    Here’s the code from my sidebar that worked with v2.3.3:

    $result = false;
    $key = “whatever”;
    if (defined(‘POC_CACHE’)) {
    $cache = new POC_Cache();
    $result = $cache->fetch($key);
    }
    if (false === $result) {
    $result = get_the_results_of_the_plugin();
    if (defined(‘POC_CACHE’)) {
    $cache->store($key, $result);
    }
    }
    echo $result;

    I’m completely stuck.

Comments are closed.