0) { if ($write) { $q = dbQuery("SELECT md5 FROM web_cache WHERE unix_timestamp(now())-last_used>$deleteOld AND rtype='$type'"); while ($r = dbFetchArray($q)) { @unlink(config::$main['cachedir'] . "/{$r[0]}.$type"); } } dbQuery("DELETE FROM web_cache WHERE unix_timestamp(now())-last_used>$deleteOld AND rtype='$type'"); } // Check for an existing entry $md5 = md5(serialize($resources[$type])); $q = dbQuery("SELECT id FROM web_cache WHERE rtype='$type' AND md5='$md5' FOR SHARE"); if ($q && dbCount($q)) { list($id) = dbFetchArray($q); return $id; } // Generate the output file if ($write && !generateResourceCache($type, $md5)) { l::warn("Resource file generation failed (type $type)"); return null; } // Add the database entry return dbQuery("INSERT INTO web_cache(rtype,md5,last_used) VALUES('$type','$md5'," . time() . ")"); } /** This function reads a resource, identified by its DB identifier, from the * database. If the resource is found in the base, it then tries to send the * file's contents. */ function displayResource($id, $rtype) { $q = dbQuery("SELECT rtype,md5 FROM web_cache WHERE id=$id FOR UPDATE"); if (!($q && dbCount($q) == 1)) { l::warn("Resource ID '$id' not in the database"); return false; } list($dbtype,$md5) = dbFetchArray($q); if ($rtype != $dbtype) { l::warn("Resource ID '$id' has wrong type $dbtype (expected $rtype)"); return false; } dbQuery("UPDATE web_cache SET last_used=" . time() . " WHERE id=$id"); endRequest(false); $path = config::$main['cachedir'] . "/$md5.$rtype"; if (readfile($path) === FALSE) { l::warn("File not found for resource '$id': $path"); return false; } exit(0); } ?>