X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=www%2Fdatabase.php;fp=www%2Fdatabase.php;h=bef3e03783394b1e807837bab28f125339992521;hb=7d9474b57575c7ca57d17b8b2d834654358d14e4;hp=0000000000000000000000000000000000000000;hpb=20cdf9418a28d56050d7d3f7bf121e87c1d31c4b;p=monitor.git diff --git a/www/database.php b/www/database.php new file mode 100644 index 0000000..bef3e03 --- /dev/null +++ b/www/database.php @@ -0,0 +1,67 @@ + +exists("production." . $name) ) + { + print "Exception: No such file %s" . $name . "\n"; + return NULL; + } + $name = "production." . $name; + $fname = $this->__file($name); + $o = unserialize(file_get_contents($fname)); + + return $o; + } + public function dump($name, $obj) + { + if ( ! file_exists(PICKLE_PATH) ) + { + if ( ! mkdir(PICKLE_PATH, 0777, True) ) + { + print "Exception: Unable to create directory :" . PICKLE_PATH . "\n"; + } + } + $name = "production." . $name; + $fname = $this->__file($name); + + return file_put_contents($fname, serialize($obj)); + } + private function __file($name) + { + return sprintf("%s/%s.phpserial", PICKLE_PATH, $name); + } + + public function exists($name) + { + return file_exists($this->__file($name)); + } + + public function remove($name) + { + return unlink($this->__file($name)); + } + + public function if_cached_else($cond, $name, $function) + { + if ( $cond and $this->exists("production.%s" % $name) ) + { + $o = $this->load($name); + } else { + $o = $function(); + if ($cond) + { + $this->dump($name, $o); # cache the object using 'name' + } + } + return o; + } +} + +?>