NanoCache: reduce queries, save time, cache
NanoCache is a very simple piece of code that'll help you with caching small pieces of content.
Let's see this example: Function
wp_list_authors is (in some occasions) able to make incredible
amount of db queries. You can wait until your hosting provider says „you are a
problem!“, you can rewrite the whole function to be more efficient (and upload
it back to Wordpress SVN – sounds right) but there is also an easier way: to
use this simple caching script.
Note: of course, problems like this probably don't touch you if you use a caching plugin like 1BlogCacher or HyperCache
Get NanoCache
- Required PHP version: 5.0 or higher
- License: GNU/GPL
- Version: 1.0
NanoCache as Wordpress plugin
Download NanoCache, a WP plugin (2 KB, RAR)
NanoCache pure code
class NanoCache {
static $dir = 'cache';
static $default_life_length = 3600; // 1 hour
public static function Update ($name, $content, $echo = false) {
$dir_path = dirname(__FILE__) . '/' . self::$dir . '/';
// maybe echo
if ($echo) echo $content;
// try to save
if (is_writable($dir_path)) {
// save to file
$file = fopen($dir_path . $name, 'w');
fwrite($file, serialize($content));
fclose($file);
return true;
} else {
// echo error
echo "\n<!-- NanoCache Update Error -->\n";
return false;
}
}
public static function Load ($name, $echo = false, $life_length = false) {
if (!$life_length) $life_length = self::$default_life_length;
$dir_path = dirname(__FILE__) . '/' . self::$dir . '/';
$file_path = $dir_path . $name;
// maybe not fount
if (!is_file($file_path)) {
echo "\n<!-- NanoCache Load Error: Not Found -->\n";
return false;
// maybe out-of-date
} elseif (time() - filemtime($file_path) > $life_length OR isset($_GET['nonanocache'])) {
echo "\n<!-- NanoCache Load Error: Out-of-date -->\n";
return false;
// maybe ok
} else {
$content = unserialize(file_get_contents($file_path));
if ($echo) echo $content . "\n<!-- NanoCache OK -->\n";
return $content;
}
}
}
Usage of NanoCache
Installation note: the cache directory needs to have
CHMOD permissions like 775 (script will create and rewrite files there).
Before: code was processed every time the page was load…
wp_list_authors(array('optioncount' => true));
Now: code is processed (defaultly) once per hour…
if (!NanoCache::Load('wp_list_authors', $echo = true)) {
NanoCache::Update('wp_list_authors', wp_list_authors(array('optioncount' => true, 'echo' => false)), $echo = true);
}
Comments
1. Sean Lindsay wrote: On July 13., 2008 comment number 1
Would it be better to wrap the logic of the ‚Load or Update‘ step into a single function, to make it easier to call? You could call this function NanoCache::Fetch. The usage would be as simple as:
NanoCache::Fetch('cache_name', function, );One less line to write each time you want to check the cache, with much less chance of coding error.
2. Sean Lindsay wrote: On July 13., 2008 comment number 2
That example should read:
NanoCache::Fetch(cache_name‘, function);3. Kahi [author] wrote: On July 13., 2008 comment number 3
↪ Sean Lindsay → You are right, I myself feel that the main „LoadAndMaybeUpdate“ code could be simplefied. Fetch sounds quite good. I'll take a look at that later…
Add a comment