Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.78 KB | None | 0 0
  1. <?php
  2.     class Ibis
  3.     {
  4.         public function getData($id)
  5.         {
  6.             if(!is_string($id))
  7.                 throw new Exception("'id' isn't a string.");
  8.  
  9.             $cacheFile = $this->_exist($id);
  10.             if(!$cacheFile)
  11.                 throw new Exception("'id' doesn't exist in cache.");
  12.  
  13.             $fp = fopen($cacheFile, "r");
  14.             if(!$fp)
  15.                 throw new Exception("'id' unable to retrieve the cache.");
  16.  
  17.             flock($fp, LOCK_UN);
  18.             $dato = fread($fp, filesize($cacheFile));
  19.             fclose($fp);
  20.        
  21.             return $dato;
  22.         }
  23.        
  24.         public function updateData($id, $value)
  25.         {
  26.             $cacheFile = $this->_exist($id);
  27.  
  28.             if($cacheFile != false)
  29.                 unlink($cacheFile);
  30.  
  31.             $fp = fopen($this->_cacheDir.md5($id), "");
  32.             if(!$fp)
  33.                 throw new Exception("Can not update 'id'.");
  34.  
  35.             flock($fp, LOCK_EX);
  36.             fwrite($fp, $value);
  37.             fclose($fp);
  38.         }
  39.        
  40.         public function isOld($id)
  41.         {
  42.             $cacheFile = $this->_exist($id);
  43.            
  44.             if((filemtime($cacheFile) + $this->_timeout) <= time())
  45.                 return false;
  46.  
  47.             return true;
  48.         }
  49.        
  50.         public function deleteId($id)
  51.         {
  52.             $cacheFile = $this->_exist($id);
  53.  
  54.             if(!$cacheFile)
  55.                 throw new Exception("'id' does not exist.");
  56.  
  57.             unlink($cacheFile);
  58.         }
  59.        
  60.         private function _exist($id)
  61.         {
  62.             $cacheFile = $this->_cacheDir.md5($id);
  63.             if(file_exist($cacheFile))
  64.                 return $cacheFile;
  65.  
  66.             return false;
  67.         }
  68.  
  69.         public function setCacheDir($cacheDir)
  70.         {
  71.             if(!is_string($cacheDir))
  72.                 throw new Exception("'cacheDir' isn't a string.");
  73.            
  74.             $this->_cacheDir = $cacheDir;
  75.         }
  76.  
  77.         public function getCacheDir()
  78.         {
  79.             return $this->_cacheDir;
  80.         }
  81.  
  82.         public function setTimeout($time)
  83.         {
  84.             if(!is_int($time))
  85.                 throw new Exception("'time' isn't a int.");
  86.  
  87.             $this->_timeout = $time;
  88.         }
  89.  
  90.         private $_cacheDir = ABSPATH."cache/";
  91.         private $_timeout;
  92.     }
  93. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement