Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Ibis
- {
- public function getData($id)
- {
- if(!is_string($id))
- throw new Exception("'id' isn't a string.");
- $cacheFile = $this->_exist($id);
- if(!$cacheFile)
- throw new Exception("'id' doesn't exist in cache.");
- $fp = fopen($cacheFile, "r");
- if(!$fp)
- throw new Exception("'id' unable to retrieve the cache.");
- flock($fp, LOCK_UN);
- $dato = fread($fp, filesize($cacheFile));
- fclose($fp);
- return $dato;
- }
- public function updateData($id, $value)
- {
- $cacheFile = $this->_exist($id);
- if($cacheFile != false)
- unlink($cacheFile);
- $fp = fopen($this->_cacheDir.md5($id), "");
- if(!$fp)
- throw new Exception("Can not update 'id'.");
- flock($fp, LOCK_EX);
- fwrite($fp, $value);
- fclose($fp);
- }
- public function isOld($id)
- {
- $cacheFile = $this->_exist($id);
- if((filemtime($cacheFile) + $this->_timeout) <= time())
- return false;
- return true;
- }
- public function deleteId($id)
- {
- $cacheFile = $this->_exist($id);
- if(!$cacheFile)
- throw new Exception("'id' does not exist.");
- unlink($cacheFile);
- }
- private function _exist($id)
- {
- $cacheFile = $this->_cacheDir.md5($id);
- if(file_exist($cacheFile))
- return $cacheFile;
- return false;
- }
- public function setCacheDir($cacheDir)
- {
- if(!is_string($cacheDir))
- throw new Exception("'cacheDir' isn't a string.");
- $this->_cacheDir = $cacheDir;
- }
- public function getCacheDir()
- {
- return $this->_cacheDir;
- }
- public function setTimeout($time)
- {
- if(!is_int($time))
- throw new Exception("'time' isn't a int.");
- $this->_timeout = $time;
- }
- private $_cacheDir = ABSPATH."cache/";
- private $_timeout;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement