Code: Select all
From Query: 0.00079894065856934
From Cache: 0.0017709732055664Code: Select all
class Raen_Cache {
protected function writeCache($file, $access, $data) {
$handle = fopen($file, $access) or die ("Raen Error: Unable to access " . $file);
fputs($handle, serialize($data));
fclose($handle);
}
protected function readCache($file) {
if (file_exists($file)) {
return @unserialize(file_get_contents($file));
} else {
$ret = "Raen Error: Cache file does not exist " . $file;
return $ret;
}
}
}
class Raen_DB extends Raen_Cache {
public function executeQuery($query) {
if (preg_match("/^SELECT/",$query)) {
$queryKey = md5($query);
$present = time();
$cacheFile = "cache/" . $queryKey . ".txt";
if (file_exists($cacheFile)) {
$fType = 'w';
} else {
$fType = 'x';
}
if (!file_exists($cacheFile) || ($present - 3600) > filemtime($cacheFile)) {
$result = mysql_query($query) or die(mysql_error());
while ($fetchArray = mysql_fetch_array($result) ) {
$sqlArray[] = $fetchArray;
}
$this->writeCache($cacheFile, $fType, $sqlArray);
return $sqlArray;
} else {
$readCache = $this->readCache($cacheFile);
return $readCache;
}
} else {
$result = mysql_query($query) or die(mysql_error());
return $result;
}
}
}