| 1 |
<?php
|
| 2 |
// $Id: fastpath_fscache.module,v 1.4 2007/07/30 20:06:52 weitzman Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* File-based caching mechanism
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_cron().
|
| 11 |
*/
|
| 12 |
function fastpath_fscache_cron() {
|
| 13 |
$cache_lifetime = variable_get('cache_lifetime', 0);
|
| 14 |
|
| 15 |
// if using file-based caching, perform routine garbage collection
|
| 16 |
if (variable_get('page_cache_fastpath', 0)) {
|
| 17 |
// delete all files older than the last call to cache_flush_all()
|
| 18 |
if (variable_get('cache_files_expired', 0)) {
|
| 19 |
$fspath = variable_get('file_cache', FASTPATH_FSCACHE_PATH);
|
| 20 |
|
| 21 |
$handle = opendir($fspath);
|
| 22 |
while (false !== ($table = readdir($handle))) {
|
| 23 |
// while (false !== ($dirmini = readdir($dirtable))) {
|
| 24 |
fastpath_fscache_purge_loop($table);
|
| 25 |
}
|
| 26 |
// closedir($dirmini);
|
| 27 |
// }
|
| 28 |
closedir($table);
|
| 29 |
|
| 30 |
variable_set('cache_files_expired', 0);
|
| 31 |
}
|
| 32 |
}
|
| 33 |
}
|
| 34 |
|
| 35 |
// Iterate over mini dirs within specified $table and send each for purging.
|
| 36 |
function fastpath_fscache_purge_loop($table) {
|
| 37 |
$fspath = variable_get('file_cache', FASTPATH_FSCACHE_PATH);
|
| 38 |
$ivalfrom = ord("a");
|
| 39 |
$ivalto = ord("f");
|
| 40 |
for($i = $ivalfrom; $i <= $ivalto; $i++) {
|
| 41 |
fastpath_fscache_purge("$fspath/$table/". chr($i));
|
| 42 |
}
|
| 43 |
|
| 44 |
for($i = 0; $i<10; $i++) {
|
| 45 |
fastpath_fscache_purge("$fspath/$table/". $i);
|
| 46 |
}
|
| 47 |
}
|
| 48 |
|
| 49 |
function fastpath_fscache_purge($dir) {
|
| 50 |
global $cache_lifetime;
|
| 51 |
$files = file_scan_directory($dir, '.', array('.', '..', 'CVS'));
|
| 52 |
foreach ($files as $file) {
|
| 53 |
if (filemtime($file->filename) < (time() - $cache_lifetime)) {
|
| 54 |
if ($fp = fopen($file->filename, 'r')) {
|
| 55 |
// We need an exclusive lock, but don't block if we can't get it as
|
| 56 |
// we can simply try again next time cron is run.
|
| 57 |
if (flock($fp, LOCK_EX|LOCK_NB)) {
|
| 58 |
unlink($file->filename);
|
| 59 |
}
|
| 60 |
}
|
| 61 |
}
|
| 62 |
}
|
| 63 |
}
|
| 64 |
|
| 65 |
/**
|
| 66 |
* Implementation of hook_exit().
|
| 67 |
*/
|
| 68 |
function fastpath_fscache_exit() {
|
| 69 |
global $base_root;
|
| 70 |
|
| 71 |
// if using fastpath caching, perform routine garbage collection based on current request URI
|
| 72 |
if (variable_get('page_cache_fastpath', 0)) {
|
| 73 |
$cache_file = cache_filename($base_root . request_uri(), 'cache_page');
|
| 74 |
if (file_exists($cache_file)) {
|
| 75 |
if ($cache_flush = variable_get('cache_flush', 0)) {
|
| 76 |
$cache_lifetime = variable_get('cache_lifetime', 0);
|
| 77 |
$mtime = filemtime($cache_file);
|
| 78 |
if (($mtime < $cache_flush) && $cache_lifetime &&
|
| 79 |
$mtime < (time() - $cache_lifetime)) {
|
| 80 |
if ($fp = fopen($cache_file, 'r')) {
|
| 81 |
if (flock($fp, LOCK_EX)) {
|
| 82 |
unlink($cache_file);
|
| 83 |
}
|
| 84 |
}
|
| 85 |
}
|
| 86 |
}
|
| 87 |
}
|
| 88 |
}
|
| 89 |
}
|