Viewing file: cacheton.php (5.83 KB) -rw-rw-r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php ############################################################################################# # � 2005 Hans Feijoo - [email protected] # # Free for you to use and abuse, it'd be nice if you did not remove this tag and mention # # me in any derivative work, but you don't have to. # # # # Cacheton - An utility to cache pages that frequently query a db which is not updated # # so frequently # # # # Requirements: Not much, PHP 4.1.0+, 5 and a cache directory with the proper permissions # # Usage: You must create a directory to store cached pages, (CACHE_DIR) with the proper # # permisions (755). Name it 'cache'. (path/to/this/file/cache/) # # Include this file at the top of your script. # # include('path/to/this/file/cacheton.php'); # # That's it! # # Comments: Default life of cache files is 300 seconds, you can change this by setting # # $cache_life before including this script or setting that value below. # # $cache_life = 60*60*24*30 //1 month! (in seconds) # # include('cacheton.php'); # # # # I'd suggest renaming cacheton.php. Files will be served even if not caching, so make sure # # cached files exist, if they don't, then THIS script is not working, usually that means a # # permission issue with your cache directory. # # If you want to know if files are actually being cached and the time they were, # # take a look at your served pages source code, at the end, there should be a comment with # # the actual caching time. # # If you need to manually clean cache files, add the argument 'flushcache', # # ie: myscript.php?arg1=foo&flushcache or cacheton.php?flushcache # # Keep in mind that ALL files in cache dir will be deleted if parameter olderthan is not # # specified. You can call it directly from this script: # # cacheton.php?flushcache&olderthan=3000 (in seconds) # # parameter olderthan is self-explanatory, if not specified, all files will be deleted # # You can prevent the caching if for instance there�s a database error or empty result, # # your db was unavailable for a certain time, you don�t want to cache 'not found' files, etc# # to prevent caching just set '$GLOBALS['nocache'] = true' anywhere in YOUR script # # It is safe to use the same cache dir and this same script across different applications. # # Just remember that when you clean cache, you clean it for all. If that�s not what you want# # you can create multiple instances of this script along with its own cache dir. # # # # This should be obvious but DO NOT use this script if the http request method is POST. # # I'd love to hear your feedback and suggestions. # # # # Enjoy it, your database will thank you. You can reduce server load up to 50% # ############################################################################################# # Last Modified 05.03.2006 # ############################################################################################# //You shouldn't need to change anything, just remember to create cache dir as a subdirectory of the directory where //this file is, with the appropiate permissions 0755 should work, if not, try 0777 //ob_start("ob_gzhandler"); //Uncomment this if you want your output to be compressed define('CACHE_DIR', dirname(__FILE__).'/cache/'); $nocache = false; $period = (!empty($cache_life) && is_numeric($cache_life)) ? round($cache_life) : 300; //cache file life in seconds $cache_file = CACHE_DIR.md5($_SERVER['REQUEST_URI']);
################################################################################# # Delete files older than cache life, comment this if not autoclean desired, # # you�ll have to clean using "flushcache", but may reduce disk and CPU load # # since it will not be done every time a script is called # autoclean($period); ################################################################################# if (!function_exists('file_put_contents')) { function file_put_contents($filename, $content) { if ($fhandler = fopen($filename, 'w')) { $result = fwrite($fhandler, $content); fclose($fhandler); return $result; } return false; } } function update($content) { $content = ob_get_contents(); if ($GLOBALS['nocache'] !== true) { //remove ".'<!-- Cached: '.date('Ymd H:i:s').' -->'" if you don�t want it in your html source code file_put_contents($GLOBALS['cache_file'], $content.'<!-- Cached: '.date('Ymd H:i:s').' -->'); } return $content; } function autoclean($age) { if (is_dir(CACHE_DIR)) { if ($dh = opendir(CACHE_DIR)) { while (($file = readdir($dh)) !== false) { if (is_file(CACHE_DIR . $file) && (time()-filemtime(CACHE_DIR.$file))>= $age) { unlink(CACHE_DIR . $file); } } } closedir($dh); } } if (isset($_REQUEST['flushcache'])) { if (is_dir(CACHE_DIR)) { if ($dh = opendir(CACHE_DIR)) { if(!empty($_REQUEST['olderthan']) && is_numeric($_REQUEST['olderthan'])){ $tcache = floor($_REQUEST['olderthan']); while (($file = readdir($dh)) !== false) { if (is_file(CACHE_DIR . $file) && (time()-filemtime(CACHE_DIR.$file))>= $tcache) { unlink(CACHE_DIR . $file); } } } else { while (($file = readdir($dh)) !== false) { if (is_file(CACHE_DIR . $file)) { unlink(CACHE_DIR . $file); } } } closedir($dh); } } } if (file_exists($cache_file)){ if((time() - filemtime($cache_file))>= $period) { ob_start('update'); } else { $content = file_get_contents($cache_file); echo $content; exit; } } else{ ob_start('update'); } ?>
|