Plugin Output Cache
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.5
. It is faster, easier to use, and also cleans up after itself when deactivated.
- 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
- 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.
- Upload the whole plugin folder to your /wp-content/plugins/ folder.
- Go to the Plugins page and activate the plugin.
- 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();
60 Comments Add your own
1. Ajay | February 12th, 2007 at 5:36 pm
Great development indeed. Btw, in the implementation above, shouldn’t you be having an else loop?
2. Rob | February 12th, 2007 at 6:29 pm
3. Everton | February 13th, 2007 at 12:11 pm
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
4. Rob | February 13th, 2007 at 4:58 pm
5. Tin | February 13th, 2007 at 8:55 pm
will this conflict with wp-cache? i’ve been using that successfully for the past year…
6. Rob | February 13th, 2007 at 10:11 pm
7. Lazy | February 14th, 2007 at 7:19 am
i`ve installed the cache plugin yesterday and run it together with wp-cache 2.1 -> works fine, nothing messed up or something like that. :)
8. Rob | February 14th, 2007 at 8:27 am
9. Lazy | February 15th, 2007 at 5:43 pm
no prob.. just a great plugin :)
10. pufaxx | February 19th, 2007 at 8:12 pm
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:
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
11. pufaxx | February 19th, 2007 at 8:22 pm
Ooops!
Some of my words words were “stolen” by “making” HTML-tags of < and the following letters. I wanted to write:
Sorry about that.
:-)
12. Rob | February 19th, 2007 at 10:12 pm
Fixed in v2.1.1
13. pufaxx | February 22nd, 2007 at 5:44 am
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 …
14. Rob | February 22nd, 2007 at 8:10 am
15. lee | April 20th, 2007 at 3:22 am
what is the difference between this plugin and wp-cache
http://mnm.uib.es/gallir/wp-cache-2/
can i use “Output Cache” with HideIt ?
http://00schmidt.com/wp/?p=306
i can use Hideit with wp-cache.but if i can use this plugin with Hideit i want switch to this plugin.
thanks.
16. Rob | April 20th, 2007 at 8:00 am
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?
17. lee | April 30th, 2007 at 11:48 am
>Rob
Thanks. I have one more question
>This plugin shouldn’t interfere with other plugins.
you mean can i use “Output Cache” with HideIt ?
http://00schmidt.com/wp/?p=306
18. Rob | April 30th, 2007 at 12:29 pm
19. Richard | June 1st, 2007 at 10:40 pm
(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. :)
20. Rob | June 2nd, 2007 at 8:04 am
21. Mark | June 21st, 2007 at 9:52 pm
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.
22. Rob | June 22nd, 2007 at 8:37 am
23. ovidiu | October 24th, 2007 at 7:23 am
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.
24. Rob | October 24th, 2007 at 9:23 am
25. ovidiu | October 24th, 2007 at 9:33 am
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.
26. Rob | October 24th, 2007 at 9:36 am
27. Rob | October 27th, 2007 at 3:00 pm
28. ovidiu | October 28th, 2007 at 9:00 am
can you help out using your plugins with wpmu?
here is a forum post regarding this plugin: http://mu.wordpress.org/forums/topic.php?id=6638&page&replies=1
thx a lot for your plugins.
29. Rob | October 28th, 2007 at 9:18 am
30. ovidiu | October 31st, 2007 at 1:16 pm
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?
31. ovidiu | November 2nd, 2007 at 8:24 am
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.
32. Rob | November 2nd, 2007 at 9:36 am
33. ovidiu | November 4th, 2007 at 9:00 pm
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,,,..
34. Diwaker | January 12th, 2008 at 3:11 am
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.
35. Rob | January 12th, 2008 at 10:16 am
36. Diwaker | January 13th, 2008 at 12:31 am
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!
37. Rob | January 13th, 2008 at 10:40 am
That’s good news!
38. Shanx | February 8th, 2008 at 4:36 am
Would this in any way conflict with WP-SuperCache?
39. Rob | February 8th, 2008 at 10:34 am
40. Fendi | February 13th, 2008 at 7:03 pm
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.
41. Rob | February 13th, 2008 at 10:12 pm
42. Fendi | February 18th, 2008 at 6:50 pm
Thanks Rob!
I’ve tried your instructions and it worked.
Do we have to clear the cache at all or does it auto-clear after a period of time?
43. Rob | February 19th, 2008 at 1:09 am
44. Fendi | February 27th, 2008 at 1:36 am
Thanks Rob.
I’m full steam ahead!
45. Rob | February 27th, 2008 at 9:36 am
46. Richard D. LeCour | March 8th, 2008 at 8:52 am
Took me about an hour to realize that the plugin wasn’t working because I had changed the default folder name from
POC_Cachetopoc-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:
(I’m hoping the above looks OK first time because you don’t have a Preview Comment button!)
47. Rob | March 8th, 2008 at 5:57 pm
Just out of interest … why did you rename the folder?
48. Richard D. LeCour | March 8th, 2008 at 8:25 pm
@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!
49. Perry | March 10th, 2008 at 8:38 pm
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.
50. Richard D. LeCour | April 26th, 2008 at 9:58 pm
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:
I’m completely stuck.
51. Rob | April 27th, 2008 at 11:12 am
52. 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.
53. Rob | April 27th, 2008 at 9:07 pm
54. 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.
55. Rob | April 28th, 2008 at 10:23 pm
56. 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
57. 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.
58. Rob | May 6th, 2008 at 9:51 am
I’m not sure about the link not working. Let’s address that after finding out about the Manage page.
59. Richard D. LeCour | May 6th, 2008 at 3:37 pm
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)
60. Rob | May 6th, 2008 at 6:13 pm
Leave a Comment
Trackback this post | Subscribe to the comments via RSS Feed