BostonWP/functionality-plugins — pull request #3 by beezee (2012-08-29 04:29:19+00:00)
Title: BWP Cache plugin

Figured I'd kick this off with a really simple development utility. This plugin simply provides a class which acts as a caching mechanism for storing data to be accessed from disparate parts of a WP codebase.

This is great for preventing duplicate queries, say when you need the same data in header.php and then in a sidebar widget. By caching the queried data in the header, the widget can access it from cache without running another query.

It's also safer than using a global variable since the cache variables cannot be modified directly, rather they must be set using the caching class setter method.

Brief documentation included in the plugin file is also pasted below. Hope this fits in with the aim of repo and hope someone finds it useful.

/*
*
* Example usage:
* $data = 'string';
* bwpCache::instance()->set('data', $data);
*
* // laterfrom widget, sidebar, footer, etc
* $data = (bwpCache::instance()->get('data')) ?: 'default';
* $data === 'string' //true
*
* Available hooks/filters:
* set_bwp_cache - as filter before setting cache. Accepts key name for cache data as second argument
* set_bwp_cache - as action before setting cache. Accepts filtered data and key name for cache data as arguments.
*
* get_bwp_cache - as filter before returning cached data. Accepts key name for cache data as second argument
* get_bwp_cache - as action before returning cached data. Accepts filtered data and key name for cache data as arguments.
*/