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. Richard D. LeCour: You are quite right that for some reason the ‘legacy’ way of using the plugin cache has stopped working. Thanks for spotting the problem. I will be looking into it. Meanwhile, try the updated method which uses a pre-built instance of the cache. It is still working!

    
    	$result = false;
    	if (defined('POC_CACHE_4')) {
    		$key = 'whatever';
    		$result = poc_cache_fetch($key);
    	}
    	if ($result === false) {
    		$result = '<br><b>hello</b>';
    		if (defined('POC_CACHE_4')) {
    			poc_cache_store($key, $result); 
    		}	
    	}	
    	echo $result;
    
  2. The management pages says “The Plugin Output Cache is ON” but “There are 0 items in the cache” which never changes. If I turn statistics on, the “Number of misses: 0 Number of hits: 0” counters never change. Refreshing any of my pages regenerates the cloud tag in the sidebar every time, one of the features I had previously successfully cached with POC 3.x and WP 2.3.3.

    I suspect that the cached data is not being stored. I have confirmed that the database is successfully created upon activation, and dropped upon deactivation.

  3. Man, I love your plugins. Very customizable as we cant see out there! Thanks a lot.

    The output cache for me is now showing the administrative page on settings.

    Whem I try to click on the link that show on the activated plugin (http://hypescience.com/wp-admin/edit.php?page=poc-cache/poc-cache-admin.php) it shows the message “Cannot load poc-cache/poc-cache-admin.php.”

    I am using wp 2.5.1, Windows 2003 web, iis6 and the issue noes not looks like permission because I gave full control to everyone on the plugin folder.

    Thank you again,

    Hype

  4. sorry! now = NOT in the comment above.

    Oh, I was not using this plugin before. I uninstalled and reinstalled it many times, always deactivating the plugin before deletion.

  5. Hype: Have you looked under the ‘Manage’ menu? That’s where the cache can be turned on or off etc.

    I’m not sure about the link not working. Let’s address that after finding out about the Manage page.

  6. it shows the message “Cannot load poc-cache/poc-cache-admin.php.”

    That’s because the folder hardcoded in the link (/poc-cache/) does not match the installation folder (/plugin-output-cache/ or /POC_Cache/ — depending on the version). Just ignore the error and visit the Manage page instead.

    (Haven’t had a chance to test your gz fix suggestion yet)

  7. Guys: There is new version of the plugin output cache out which fixes the stray link and, I hope, fixes Richard’s bug too. (Richard, please let me know!)

  8. My PHP error logs are filling up.

    
    [Tue May 20 12:02:45 2008] [error] [client 74.61.80.117] PHP Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/shop/public_html/ca/wp-content/plugins/plugin-output-cache/poc-cache.php on line 105, referer: http://ca.shop-network.org/
    

  9. I’ve just come upon some suspect behaviour:
    I have Recent Comments set in the sidebar, and another output under the post excerpt (thank you for so graciously adding this functionality, it is working perfectly). However, when enabling the output cache, only the sidebar output is displayed, the loop recent comments just disappear (cache stats indicate it is serving output to your various plugins).

    Is this intended behaviour? Am I doing something wrong?

  10. Still doesn’t display the front page output (I tried logged in and out). If anything the performance seems to have decreased – was 100% efficient previously, now it seems about 85%.

  11. Artefact/Perihelion: I’m glad to hear about the efficiency — the way it is defined it can never be 100% since the it takes a cache miss to load the item into the cache.

    Can you leave me a bug report from the Recent Comments settings screen? That will give me some extra debugging info. Can you also add the parameter string you use for the call under the posts? I’ll see if I can duplicate the cache problem and then have a chance to solve it.

  12. Hello Rob!

    I tested POC with your Similar Posts plugin and some other plugins on localhost, and it worked perfectly. However, when I activated POC on my remote server, I faced the exact problem mentioned by Richard in comment no. 54.

    I checked the database, and I also found out that the cached data is not being stored. I was wondering how Richard solved the issue, and hoping you could guide me through it. (I am using the version 4.0.6.)

    Thanks. 🙂

  13. Saadat: That’s an interesting puzzle. Why would it work locally but not on your remote server? Strange. Could you let me know some details of the two setups (OS, server, PHP, MySQL)?

  14. Rob: These are the details of both setups.

    localhost: XAMPPlite version 1.6.5
    OS: Windows XP
    Server: Apache 2.2.6
    PHP 5.2.5
    MySQL 5.0.51.

    Remote server:
    OS: Linux
    Server: Apache 1.3.39
    PHP 4.4.7
    MySQL 4.1.22.

    Could it be that they need to upgrade at my remote server?

  15. Hi Rob,

    I had writen a contact message / comment about 1+ month ago, but couldn’t hear anything so far. I’m in urgent need of your help for optimizing my db queries. Actuall I’m facing lots of problems for the past 1+ months, after moving to DreamHost VPS.

    I’ve tried to use your POC-Cache plugin with some WordPress built-in function, but couldn’t let it run.

    May I request you to give me an example for the following function, so that I can try on my own for other functions as well

    <?php $posts = get_posts('numberposts=5&offset=8'); foreach($posts as $post) : setup_postdata($post); ?><li>................</li>
    <?php endforeach; ?>

    Or you can give a live example of wp-list-pages function.

    Hope for an early positive reply.

    Thansk,

    DG…

  16. DG: The idea is simple: choose a key that uniquely identifies what you want to cache; use it to check if the data is already cached; if not go ahead and generate the data the slow way and also cache it with the key.

    e.g.: (untested)

    
    // the 'echo=0' returns the output instead of echoing it
    $args = 'exclude=1,3&echo=0';
    $key = 'wplp'.$args;
    $data = poc_cache_fetch($key);
    if (!$data) {
      $data = wp_list_pages($args);
      poc_cache_store($key, $data)
    }
    echo $data;
    

    I hope that gives you the idea.

  17. Saadat: It could be a MySQL version issue but I can’t spot which queries might be at fault. Do you have access to your PHP error logs? That might give a clue. Meanwhile I’ll delve about a bit in the code.

  18. Rob: Unfortunately, I don’t have access to PHP error logs since I use shared hosting. Though I did try to log any errors (using this script), but couldn’t find any entry from poc-cache.php.

  19. I seem to have the same issues as Richard. Nothing is being written to the table. I can see the table being dropped & created no problem on activate/deactivate. I’m only trying to make it work with ‘Similar Posts’.

    WP 2.6
    POC 4.0.6

  20. Jeff: Do you have access to your PHP error log? If so could see if there are any errors specific to the cache plugin? That would be a good place to start hunting down this issue. Thanks.

  21. Hello, I\’ve just started to use this plugin with two of your other plugins (Compact Archives and Blix Archives) and it looks promising.

    The problem is that when the post titles from the Blix Archives are cached, non-ASCI characters like \’ÅŸ\’ and \’Å£\’ turn into question marks.

    Is there any way to correct this?

  22. I found out that if you just change line 108 in poc-cache-admin.php (version 4.0.6)

    from

    ) ENGINE = MyISAM;\";

    to

    ) ENGINE = MyISAM DEFAULT CHARSET=utf8;\";

    it solves the problem.

  23. Hello Rob!

    I had previously left a comment (#68) about a problem with the POC plugin. My web host recently upgraded their PHP and MySQL setups, and the plugin is working prefectly fine now. I guess it really was a version issue, after all.

    Thanks again for your excellent work. 🙂

  24. Hello!
    Your plugin is great!
    I’ve made the .po file in french but the translation doesn’t work because the function “load_text_domain” is not include. 🙁

    Can you make this change in your next versions?

    Regards…

  25. Hi. Fantastic plugin that I have been using for a while.

    Here’s my question, I have a function ready to strip the content of blockquote tags from the excerpt, just because I normally have a quote at the beginning of my posts that usually takes most of the whole thing.

    Is there a way to pass the output of this plugin through a filter so that I can apply my function? Having a custom plugin of mine on top of yours would be better than editing manually your files.

Comments are closed.