22/11: Caching Solutions for Nucleus CMS
I have been using the Nucleus content management system for a few years, and I really like it. I once installed the Drupal on my Dreamhost server, and it was really slow compared to nucleus. The nucleus core is lightweight which makes it pretty fast, but not fast enough for my sluggish server though. It still makes a lot of SQL queries and has to read and parse templates which takes a lot of time. So how do we solve this problem you ask? One solution is; by caching your web page server side.
There are 2 plugins available for caching that I know of:
I never used the NP_CacheLite, but I took a look at the code and I didn't really like it. This plugin gives you the possibility of putting dynamic content between no cache tags in your skin, which means it still has to fetch your skin from your database. Maybe it's good for some websites, but not for this one.
I went for the NP_Cache plugin, a fully cached solution for your website. Using a full cache of your web page is great but it comes with some glitches. Everything is static, this means the cache file has to be deleted every time a visitor comments on a item, or gives a rating, or when a user logs in for example. So everything which used to be dynamic has ceased to work. A few examples of plugins which won't work are NP_Captcha, NP_Views, NP_ Refer. You can address this issue by getting some dynamic content through an AJAX request.
Below is my customized code for NP_Cache:
Notice the $dont_cache array. If a string value of the array occurs in the url of the web page the page won't be cached. You should alter this array to whatever fits you.
You can download the customized plugin here: NP_Cache
How to install and use the plugin?
Download the plugin above and save it to a file called NP_Cache.php in your nucleus/plugins directory.
Step 1 Install NP_Cache.php from the Plugin Management page of the Nucleus admin area. After installing, edit the options for the NP_Cache plugin from that same page. Set the cache dir to match the absolute directory on your server. This directory must exist and must be writable by the web server. For Example: /home/myname/www.mysite.com/cache
Step 2 This is the code you want to add to the beginning of your root index.php and for each blog. Replace the “/home/myname/www.mysite.com/cache” with the same directory path you set in the plugin options, be sure to surround it in quotes. Add it directly after the <?php line
That's it, if everything went ok you will have a noticeable faster website. On the left is a speed graph of this website measured by Safari. The load time went from approximately 8.28 sec to 4.69 seconds with NP_Cache.
There are 2 plugins available for caching that I know of:
I never used the NP_CacheLite, but I took a look at the code and I didn't really like it. This plugin gives you the possibility of putting dynamic content between no cache tags in your skin, which means it still has to fetch your skin from your database. Maybe it's good for some websites, but not for this one.
I went for the NP_Cache plugin, a fully cached solution for your website. Using a full cache of your web page is great but it comes with some glitches. Everything is static, this means the cache file has to be deleted every time a visitor comments on a item, or gives a rating, or when a user logs in for example. So everything which used to be dynamic has ceased to work. A few examples of plugins which won't work are NP_Captcha, NP_Views, NP_ Refer. You can address this issue by getting some dynamic content through an AJAX request.
Below is my customized code for NP_Cache:
<?php
class NP_Cache extends NucleusPlugin {
var $cache_dir = '';
function getMinNucleusVersion() { return 155; }
function getName() { return 'Nucleus Cache'; }
function getAuthor() { return 'Jim Burnett | Wesley Luyten'; }
function getURL() { return 'http://www.mediauncut.com/'; }
function getVersion() { return '1.1.01'; }
function getDescription() { return 'Enables caching of nucleus items, homepage, searches and anything off index.php.'; }
function getEventList() {
return array('PreSkinParse','PostSkinParse','PostAddItem','PreUpdateItem');
}
function install() {
$this->createOption('cache_dir','Enter the path (no trailing slash) of the cache directory: ','text','/tmp');
}
function init() {
$this->cache_dir = trim($this->getOption('cache_dir'));
$this->obStarted = 0;
}
function event_PreSkinParse(&$data) {
ob_start();
$bStart = 1;
// don't start output buffering if we're not at the top level
// (fixes problems with RSS feeds & ETAG)
if(function_exists('ob_get_level') && (ob_get_level() > 0))
$bStart = 0;
if($bStart)
$this->obStarted = ob_start("ob_gzhandler");
}
function event_PostAddItem(&$data) {
$blogid = getBlogIDFromItemID($data['itemid']);
$url = createItemLink($data['itemid']);
$this->deleteCacheFile($url);
$url = createBlogidLink($blogid);
$this->deleteCacheFile($url);
}
function event_PreUpdateItem(&$data) {
$blogid = getBlogIDFromItemID($data['itemid']);
$url = createItemLink($data['itemid']);
$this->deleteCacheFile($url);
$url = createBlogidLink($blogid);
$this->deleteCacheFile($url);
}
function deleteCacheFile($file) {
$file_name = $this->convertURL($file);
$cache_file = $this->cache_dir.'/cache-'.$file_name;
if(file_exists($cache_file)) unlink($cache_file);
$cache_file = $this->cache_dir.'/cache-';
if(file_exists($cache_file)) unlink($cache_file);
}
function event_PostSkinParse(&$data) {
$url = getenv('REQUEST_URI');
$file_name = $this->convertURL($url);
$cache_file = $this->cache_dir.'/cache-'.$file_name;
// if these string values occur in the url don't cache the page
$dont_cache = array('php','action','query','catid','category','archive','gif','png','jpg','member');
str_ireplace($dont_cache,'',$file_name,$count);
if($count==0)
{
if(!$handle = fopen($cache_file,'w')) {
echo "Cannot open file ($cache_file)";
exit;
}
if(fwrite($handle, ob_get_contents()) === false) {
echo "Cannot write to file ($cache_file)";
exit;
}
}
ob_end_flush();
}
function convertURL($a) {
global $CONF;
$replace = array($CONF['IndexURL'],'/','&','?','=');
return str_replace($replace,'',$a);
}
}
?>
class NP_Cache extends NucleusPlugin {
var $cache_dir = '';
function getMinNucleusVersion() { return 155; }
function getName() { return 'Nucleus Cache'; }
function getAuthor() { return 'Jim Burnett | Wesley Luyten'; }
function getURL() { return 'http://www.mediauncut.com/'; }
function getVersion() { return '1.1.01'; }
function getDescription() { return 'Enables caching of nucleus items, homepage, searches and anything off index.php.'; }
function getEventList() {
return array('PreSkinParse','PostSkinParse','PostAddItem','PreUpdateItem');
}
function install() {
$this->createOption('cache_dir','Enter the path (no trailing slash) of the cache directory: ','text','/tmp');
}
function init() {
$this->cache_dir = trim($this->getOption('cache_dir'));
$this->obStarted = 0;
}
function event_PreSkinParse(&$data) {
ob_start();
$bStart = 1;
// don't start output buffering if we're not at the top level
// (fixes problems with RSS feeds & ETAG)
if(function_exists('ob_get_level') && (ob_get_level() > 0))
$bStart = 0;
if($bStart)
$this->obStarted = ob_start("ob_gzhandler");
}
function event_PostAddItem(&$data) {
$blogid = getBlogIDFromItemID($data['itemid']);
$url = createItemLink($data['itemid']);
$this->deleteCacheFile($url);
$url = createBlogidLink($blogid);
$this->deleteCacheFile($url);
}
function event_PreUpdateItem(&$data) {
$blogid = getBlogIDFromItemID($data['itemid']);
$url = createItemLink($data['itemid']);
$this->deleteCacheFile($url);
$url = createBlogidLink($blogid);
$this->deleteCacheFile($url);
}
function deleteCacheFile($file) {
$file_name = $this->convertURL($file);
$cache_file = $this->cache_dir.'/cache-'.$file_name;
if(file_exists($cache_file)) unlink($cache_file);
$cache_file = $this->cache_dir.'/cache-';
if(file_exists($cache_file)) unlink($cache_file);
}
function event_PostSkinParse(&$data) {
$url = getenv('REQUEST_URI');
$file_name = $this->convertURL($url);
$cache_file = $this->cache_dir.'/cache-'.$file_name;
// if these string values occur in the url don't cache the page
$dont_cache = array('php','action','query','catid','category','archive','gif','png','jpg','member');
str_ireplace($dont_cache,'',$file_name,$count);
if($count==0)
{
if(!$handle = fopen($cache_file,'w')) {
echo "Cannot open file ($cache_file)";
exit;
}
if(fwrite($handle, ob_get_contents()) === false) {
echo "Cannot write to file ($cache_file)";
exit;
}
}
ob_end_flush();
}
function convertURL($a) {
global $CONF;
$replace = array($CONF['IndexURL'],'/','&','?','=');
return str_replace($replace,'',$a);
}
}
?>
Notice the $dont_cache array. If a string value of the array occurs in the url of the web page the page won't be cached. You should alter this array to whatever fits you.
You can download the customized plugin here: NP_Cache
How to install and use the plugin?
Download the plugin above and save it to a file called NP_Cache.php in your nucleus/plugins directory.
Step 1 Install NP_Cache.php from the Plugin Management page of the Nucleus admin area. After installing, edit the options for the NP_Cache plugin from that same page. Set the cache dir to match the absolute directory on your server. This directory must exist and must be writable by the web server. For Example: /home/myname/www.mysite.com/cache
Step 2 This is the code you want to add to the beginning of your root index.php and for each blog. Replace the “/home/myname/www.mysite.com/cache” with the same directory path you set in the plugin options, be sure to surround it in quotes. Add it directly after the <?php line
$cache = '/home/myname/www.mysite.com/cache/cache-'.str_replace(array('/','&','?','='),'',getenv('REQUEST_URI'));
$t = 86400; // cache files will be deleted every 1 day = 24*60*60
// delete cache file when someone comments
if($_REQUEST['action'] == 'addcomment') unlink($cache);
elseif(file_exists($cache) && (time() - $t < filemtime($cache)))
{
echo file_get_contents($cache);
echo '<!-- From cache generated '.date('H:i',filemtime($cache)).' by NP_Cache -->';
exit();
}
$t = 86400; // cache files will be deleted every 1 day = 24*60*60
// delete cache file when someone comments
if($_REQUEST['action'] == 'addcomment') unlink($cache);
elseif(file_exists($cache) && (time() - $t < filemtime($cache)))
{
echo file_get_contents($cache);
echo '<!-- From cache generated '.date('H:i',filemtime($cache)).' by NP_Cache -->';
exit();
}
That's it, if everything went ok you will have a noticeable faster website. On the left is a speed graph of this website measured by Safari. The load time went from approximately 8.28 sec to 4.69 seconds with NP_Cache.


I am Wesley Luyten, on the net I'm also known as wessite. I am born and raised in Belgium, but I also have Canadian blood running through my body. I'm currently learning for my degree in engineer ICT, and I'm pretty much web addicted. I use a macbook pro to handle my computer business.