Advertisement
Guest User

Untitled

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