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.

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 Comments

  • 1. Rob  |  April 27th, 2008 at 11:12 am

    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. Richard D. LeCour  |  April 27th, 2008 at 6:40 pm

    I tried exactly what you have above several times yesterday, and still couldn’t get the plugin to work.

  • 3. Rob  |  April 27th, 2008 at 9:07 pm

    Richard: That’s bad news because I have it working here. What does the plugin cache management page say?

  • 4. Richard D. LeCour  |  April 28th, 2008 at 5:15 pm

    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.

  • 5. Rob  |  April 28th, 2008 at 10:23 pm

    Richard: Thanks for the extra info. I’ll try and track down the problem as soon as I can.

  • 6. Hype  |  May 6th, 2008 at 3:05 am

    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

  • 7. Hype  |  May 6th, 2008 at 3:07 am

    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.

  • 8. Rob  |  May 6th, 2008 at 9:51 am

    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.

  • 9. Richard D. LeCour  |  May 6th, 2008 at 3:37 pm

    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)

  • 10. Rob  |  May 6th, 2008 at 6:13 pm

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

  • 11. Tony  |  May 20th, 2008 at 6:08 pm

    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/
    

  • 12. Rob  |  May 20th, 2008 at 11:11 pm

    Tony: Thanks for the report. I’ve made a code change which I think eliminates the warning message and sent it to you to test out.

  • 13. Perihelion  |  May 28th, 2008 at 9:34 pm

    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?

  • 14. Rob  |  May 29th, 2008 at 12:13 am

    Perihelion: That does sound suspect! I’ll look into it after a night’s sleep…

  • 15. Rob  |  May 29th, 2008 at 10:47 am

    Perihelion: I’ve sent you by email a version that might correct your problem. If it does I’ll release it.

  • 16. Artefact  |  May 29th, 2008 at 8:27 pm

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

  • 17. Rob  |  May 29th, 2008 at 9:31 pm

    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.

  • 18. Saadat  |  June 14th, 2008 at 11:08 pm

    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. πŸ™‚

  • 19. Rob  |  June 15th, 2008 at 10:05 am

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

  • 20. Saadat  |  June 15th, 2008 at 11:54 am

    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?

  • 21. DG  |  June 15th, 2008 at 11:56 am

    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…

  • 22. Rob  |  June 15th, 2008 at 3:07 pm

    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.

  • 23. Rob  |  June 15th, 2008 at 3:19 pm

    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.

  • 24. Saadat  |  June 16th, 2008 at 5:56 am

    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.

  • 25. Jeff  |  August 10th, 2008 at 8:22 am

    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

  • 26. Rob  |  August 10th, 2008 at 10:00 am

    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.

  • 27. Jeff  |  August 11th, 2008 at 2:26 am

    Sadly nothing in the php error log about this plugin at all.

  • 28. Rob  |  August 11th, 2008 at 8:41 am

    Jeff: I’ve emailed you a version of the plugin with some debugging code included. Hopefully we will be enlightened.

  • 29. scribu  |  August 27th, 2008 at 4:30 pm

    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?

  • 30. scribu  |  August 27th, 2008 at 4:43 pm

    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.

  • 31. Rob  |  August 27th, 2008 at 8:49 pm

    scribu: Thank you for the bug report … and thank you even more for finding the solution! I’ll make the change and release.

  • 32. Saadat  |  October 24th, 2008 at 11:36 am

    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. πŸ™‚

  • 33. Yin-YIn  |  November 9th, 2008 at 6:10 pm

    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…

  • 34. Enclave  |  July 4th, 2009 at 7:43 am

    Is this still being maintained?

  • 35. nonhocapito  |  July 4th, 2009 at 8:45 pm

    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.