function generate_browse() { // check if there is anything cached $cached = load_cached('browse'); if ($cached !== false) { // yes, output echo $cached; flush(); } // check if we still want/need to generate the page if (cache_needs_update('browse', config('cache_time'))) { $data = array(); foreach (list_works() as $name) { $work = load_work($name); if ($work === false) { continue; } $data[] = $work; } $generated = run_template('template-browse.php', $data); // save the generated page save_cached('browse', $generated); // output if we haven't already above if ($cached === false) { echo $generated; } } }
function cache_or_disk($keyword, $backing_file, $cache_keys) { // fb("cache_or_disk({$keyword}, {$backing_file}, ..."); // fb($cache_keys, "cache_keys"); $known_keywords = array('objects', 'status', 'perms'); if (!in_array($keyword, $known_keywords)) { // XXX do something better die("Unknown keyword '{$keyword}'"); } $useAPC = useAPC(); $start_time = microtime(TRUE); $array = NULL; //fb($keyword, "doing cache_or_disk($keyword)"); if ($useAPC) { //fb(apc_cache_info(), "apc cache info"); $read_file = cache_needs_update($keyword, $backing_file); $cacheFail = FALSE; $success = FALSE; if (!$read_file) { // Loop through each variables cached value. If a read fails note it and // read from disk foreach ($cache_keys as $key) { $array[$key] = apc_fetch($key, $success); if (!$success) { $cacheFail = TRUE; //fb("Cache Fail for key {$key}!"); break; } } if (!$cacheFail) { // Every key was found in cache //fb("$keyword data from cache!"); } } } // The cache is not enabled or // the cache is enabled and of the following three conditions occurred // There was a cache miss // The data file is newer than the cached version if (!$useAPC || !$success || $read_file || $cacheFail) { $array = read_disk($keyword, $cache_keys); if ($useAPC) { //foreach($cache_keys as $key) { foreach (array_keys($array) as $key) { apc_store($key, $array[$key]); } apc_store('last_' . $keyword . '_read', time()); //fb('stored keys to cache'); } //fb("$keyword data from disk!"); } $end_time = microtime(TRUE); //fb($end_time - $start_time, "Elapsed load time {$end_time} - {$start_time}"); return $array; }