How does the default recent posts widget work with cache?

I am trying to build a widget based on the default wordpress recent posts widget. In wp-includes/default-widget.php , I noticed in class WP_Widget_Recent_Posts in function widget(), the recent posts are first looked for in cache and if not found, then output buffering is turned on and the result is generated and then stored back into the cache. Since I am using NuSphere IDE , I put a DebugBreak(); inside the condition
if ( isset( $cache[ $args['widget_id'] ] ) ) (DebugBreak() causes a breakpoint exception to occur in the current process.) but the breakpoint was never executed pointing out that the cache wasn’t set before. Now this shouldn’t be the case as the cache does get set by wp_cache_set('widget_recent_posts', $cache, 'widget');. This is my first dilemma.

Secondly, I would really appreciate if someone threw some light on how output buffering works. Cause as I mentioned before, the recent posts are first looked for in cache and if not present there then output buffering is turned on (ob_start();), result is fetched and then ob_get_flush(); is performed like $cache[$args['widget_id']] = ob_get_flush(); and the cache is set. So if no output is echoed to the browser, then how does the widget actually display the recent posts? PLEASE HELP!

1 Answer
1

Unless you have a memcached-type plugin installed, wp_cache_set will only store data for the duration of the current script. Call or add the widget again in the same instance & you’ll see it utilise the cache.

As for ob_get_flush(), taken from the manual:

Flush the output buffer, return it as a string and turn off output buffering

In other words, it prints and returns the buffer at the same time.

Leave a Comment