Acme Web Design Info


PHP-Cache-Kit - dramatically speed up your site with this easy-to-use PHP caching kit

php-cache-kit sshot

PHP-Cache-Kit is a slim little PHP class which allow you to quickly and easily implement module-level caching into your PHP projects.

With some thoughtful caching, your site can scale upwards dramatically with existing infrastructure. Once you see it in action and understand how simple caching is to implement, you'll surely find lots of uses. PHP-Cache-Kit is easy to understand and simplified down to under a hundred lines of code.

Easy to integrate, flexible and blazingly fast - oh, and free too!

How to use PHP-Cache-Kit

Include cache-kit.php in your project and set the two global variables cache_active=true and cache_folder=(your desired cache repository folder)!

Now the trick is to identify self-contained modules that require lots of computation or time and yet do not need to be rebuilt every single time they are loaded. For example, a calendar widget might change only once a day so why rebuild it (will all the related DB calls etc) every single time? Instead, cache it with a refresh time of 20 minutes. Then it will be rebuild every twenty minutes only. If 500 people load the page during that time, they will get images served directly from the very very fast cache. Who cares it the calendar does not refresh until a few minutes after midnight.

Here's a little 10-second cached "hello world" example:

<?php
 
  
// Usage example:
 
  // Do this stuff in your config file
  
include_once('cache-kit.php');
  
$cache_active true
  
$cache_folder 'cache/';  
 
  
// Now you can convert any time-consuming but rarely-changing data module into 
  // a fast cached module. This example rebuilds the calendar only every 5 minutes. 
  
function helloWorld(){
   
$result acmeCache::fetch('helloWorld'10); // 10 seconds
   
if(!$result){
    
$result '<h2> Hello world</h2> <p>Build time: '.date("F j, Y, g:i a").'</p>';
    
acmeCache::save('helloWorld'$result);
   } else echo(
'Cached result<br/>'); 
   return 
$result;
  }

  
// now use the content module function just like you normally would -- caching is automatic!
  
echo(helloWorld());
 
?>

Study this caching class (below) for a moment until you see how simple caching can actually be. Most of the operations are read operations so the actual chance of write-conflicts are very slim.

Notice that you do not actually have to create an object. The cache code is wrapped in a class simply as a mechanism to avoid namespace conflicts with your existing code. All you ever have to call is acme_cache::save() and acme_cache::fetch(). Here is the entire code. As you can see, it's very compact.

Download PHP-Cache-Kit >>>

<?php



class acmeCache

 
// public functionality, acmeCache::fetch() and acmeCache::save()
 // =========================

 
function fetch($name$refreshSeconds 0){
  if(!
$GLOBALS['cache_active']) return false
  if(!
$refreshSeconds$refreshSeconds 60
  
$cacheFile acmeCache::cachePath($name); 
  if(
file_exists($cacheFile) and
   ((
time()-filemtime($cacheFile))< $refreshSeconds)) 
   
$cacheContent file_get_contents($cacheFile);
  return 
$cacheContent;
 } 
 
 function 
save($name$cacheContent){
  if(!
$GLOBALS['cache_active']) return; 
  
$cacheFile acmeCache::cachePath($name);
  
acmeCache::savetofile($cacheFile$cacheContent);
 } 

 
// for internal use 
 // ====================
 
function cachePath($name){
  
$cacheFolder $GLOBALS['cache_folder'];
  if(!
$cacheFolder$cacheFolder trim($_SERVER['DOCUMENT_ROOT'],'/').'/cache/';
  return 
$cacheFolder md5(strtolower(trim($name))) . '.cache';
 }
 
 function 
savetofile($filename$data){
  
$dir trim(dirname($filename),'/').'/'
  
acmeCache::forceDirectory($dir);  
  
$file fopen($filename'w');
  
fwrite($file$data); fclose($file);
 } 
  
 function 
forceDirectory($dir){ // force directory structure 
  
return is_dir($dir) or (acmeCache::forceDirectory(dirname($dir)) and mkdir($dir0777));
 }

}
?>

That's it, once you try it you'll be hooked. Feel free to modify and use the code in any way that you like. If you find the code very useful, don't forget to send people our way by linking to this page. ;)


Looking for other cool web design tools?

Check out the super cool graphics counter you can use on your website (no signup required): Web-Traffic-Counter

Or check out some other popular pages: