function shLoadURLCache() { global $shURLDiskCache, $shURLCacheFileName, $shURLTotalCount, $shURLMemCache, $shURLRam, $shCacheFileLocked; static $shDiskCacheLoaded = false; if (!$shDiskCacheLoaded) { $shCacheFileLocked = false; _log('Cache not loaded - trying to load ' . $shURLCacheFileName); if (file_exists($shURLCacheFileName)) { $startMem = function_exists('memory_get_usage') ? memory_get_usage() : 'unavailable'; _log('Including cache file (mem = ' . $startMem . ')'); $GLOBALS['shURLDiskCache'] = array(); // erase global, not local copy $lock =& Shlock::getInstance(); // we try lock the cache file until the end of the request // so as to avoid other concurrent requests writing to it // while we have some pending data if ($lock && $lock->acquire('shCacheContent')) { $shCacheFileLocked = true; } include $shURLCacheFileName; $endMem = function_exists('memory_get_usage') ? memory_get_usage() : 'unavailable'; $shURLRam = $startMem == 'unavailable' ? $startMem : $endMem - $startMem; $shDiskCacheLoaded = !empty($shURLDiskCache); $shURLTotalCount = !empty($shURLDiskCache) ? count($shURLDiskCache) : 0; _log('Cache file included : ' . ($startMem == 'unavailable' ? $startMem : $endMem - $startMem) . ' bytes used, ' . $shURLTotalCount . ' URLs'); } else { // cache file not there, create it $now = time(); $sefConfig =& Sh404sefFactory::getConfig(); $cache = '<?php // shCache : URL cache file for sh404SEF //' . $sefConfig->version . ' if (!defined(\'_JEXEC\')) die(\'Direct Access to this location is not allowed.\'); $shURLCacheCreationDate = ' . $now . ';' . "\n"; $cache .= "\n" . '?' . '>'; // lock cache file before using it $lock =& Shlock::getInstance(); if ($lock && $lock->acquire('shCacheContent')) { $shCacheFileLocked = true; $cacheFile = fopen($shURLCacheFileName, 'ab'); if ($cacheFile) { fwrite($cacheFile, $cache); fclose($cacheFile); } } $GLOBALS['shURLDiskCache'] = array(); $shDiskCacheLoaded = true; // we don't want to try again if it failed first time _log('Cache file does not exists'); } } }
function shWriteURLCacheToDisk() { global $shURLDiskCache, $shURLMemCache, $shURLCacheFileName, $shURLCacheCreationDate; $sefConfig =& shRouter::shGetConfig(); if (!count($shURLMemCache)) { return; } // nothing to do, no new URL to write to disk $now = time(); if (!file_exists($shURLCacheFileName)) { $cache = '<?php // shCache : URL cache file for sh404SEF //' . $sefConfig->version . ' if (!defined(\'_JEXEC\')) die(\'Direct Access to this location is not allowed.\'); $shURLCacheCreationDate = ' . $now . ';' . "\n"; } else { $cache = '<?php' . "\n"; // check cache TTL if (empty($shURLCacheCreationDate)) { // file exists, but creation date is missing : we are upgrading from a previous version $status = stat($shURLCacheFileName); // lets's read from file status : use last change date as creation date if (!empty($status)) { $shURLCacheCreationDate = $status[9]; $cache .= "\n" . '$shURLCacheCreationDate=' . $shURLCacheCreationDate . ";\n"; } } if (SH404SEF_URL_CACHE_TTL && mt_rand(1, SH404SEF_URL_CACHE_WRITES_TO_CHECK_TTL) == 1) { // probability = 1/SH404SEF_WRITES_TO_CLEAN_LOGS if (!empty($shURLCacheCreationDate)) { // if we have a valid creation date, check TTL if ($now - $shURLCacheCreationDate > SH404SEF_URL_CACHE_TTL * 86400) { // cache must be cleared $GLOBALS['shURLDiskCache'] = array(); unlink($shURLCacheFileName); $shURLCacheCreationDate = $now; $cache = '<?php // shCache : URL cache file for sh404SEF //' . $sefConfig->version . ' if (!defined(\'_JEXEC\')) die(\'Direct Access to this location is not allowed.\'); $shURLCacheCreationDate = ' . $now . ';' . "\n"; } } } } $count = count($shURLDiskCache); $cache .= sh_var_export($shURLMemCache, $count); // only need to write memory cache, ie: those URL added since last read of cache from disk $cache .= "\n" . '?' . '>'; // lock cache file before using it $lock =& Shlock::getInstance(); if ($lock && $lock->acquire('shCacheContent')) { $cacheFile = fopen($shURLCacheFileName, 'ab'); if ($cacheFile) { fwrite($cacheFile, $cache); fclose($cacheFile); } $lock->release('shCacheContent'); } }