How Creating the Functions Cach on WordPress Sidebar

Before we can write our function, we need to understand exactly what we’re trying to do. Our goal is to cache our sidebar to a text file and update that cache whenever we publish a post, change our theme or make changes to the sidebar widgets. Since we’re planning on using our cache to display the sidebar on a different application, we need to be able to easily delete the cache without any ill-effects (we don’t want our sidebar broken at any time).

To accomplish this efficiently, we’ll also create a log of our cached files and use that to determine whether the cache should be cleared. If so, the actual cache file will be overwritten the next time someone visits the WordPress site, ensuring a proper sidebar is always rendered on both applications.

Creating the Functions Caching on Your WordPress  Sidebar

To get started, open up your theme’s functions.php file and add the following functions to it:

function cache($task, $cacheFile, $cacheTime = 21600){

global $cache;

// Configure files and directories:

$cacheDir = TEMPLATEPATH."/cache";

$cacheFileName = $cacheDir."/cache-$cacheFile.txt";

$cacheLogFile = $cacheDir."/cache-log.txt";

// Make cache directory if it doesn't exist

if(!is_dir($cacheDir)) mkdir($cacheDir, 0755);

// Make a log of the cache files with their current status

if(file_exists($cacheLogFile))

$cacheLog = unserialize(file_get_contents($cacheLogFile));

else

$cacheLog = array();

if($task == 'start'){

// If cache exists, is less than 6 hours old and is not in deletion queue, keep it - otherwise rebuild cache

if(file_exists($cacheFileName) && (time() - filemtime($cacheFileName)) < $cacheTime && $cacheLog[$cacheFile] == 1){

$cache = false;

} else {

$cache = true;

ob_start();

}

}elseif($task == 'end' && $cache){

// If caching, save file contents and update log

file_put_contents($cacheFileName,ob_get_contents());

ob_end_flush();

$cacheLog[$cacheFile] = 1;

file_put_contents($cacheLogFile,serialize($cacheLog));

}elseif($task == 'purge'){

// Set cache to delete and update log

$cacheLog[$cacheFile] = 0;

file_put_contents($cacheLogFile,serialize($cacheLog));

}

}

function cache_purge(){

$cacheDir = TEMPLATEPATH."/cache";

$cacheLogFile = $cacheDir."/cache-log.txt";

if(file_exists($cacheLogFile))

$cacheLog = unserialize(file_get_contents($cacheLogFile));

else

$cacheLog = array();

foreach($cacheLog as $key=>$value)

$cacheLog[$key] = 0;

file_put_contents($cacheLogFile,serialize($cacheLog));

}

The second function is used to purge all cache files and is useful for adding to existing WordPress hooks. With it, we can make it so our cache is cleared every time our theme is updated, a post is saved or our sidebar widgets are updated by adding the following at the end of functions.php:

<?php cache('start', 'sidebar'); ?>

<?php cache('end', 'sidebar'); ?>

Using Your Cache

Now that you’ve created your cache file, you can use it in any application that has access to it. You can easily do this using the include_once function but I also like making sure the file exists, just to be safe:

<?php
$myCachedFile = "/path/to/cache-sidebar.txt";
if(file_exists($myCachedFile))
include_once($myCachedFile);
else
echo "Cache doesn't exist.";
?>

Just be sure to double-check your file path is correct. If you want to set your cache to delete from an external application, you’ll have to include the cache_purge function in your script somewhere and simply call that whenever necessary. After that it’s simply a matter of calling the function:

<?php purge_cache(); ?>