Jeff PHP framework  0.99
Modular, extensible, OOP, MVC, lightweight php framework designed to ease the programmers in the development of web applications.
cache.php
Go to the documentation of this file.
00001 <?php
00028 class cache {
00029 
00033         protected $_ds;
00034         
00038         protected $_fld;
00039         
00043         protected $_prefix;
00044         
00048         protected $_grp;
00049         
00053         protected $id;
00054         
00058         protected $_tc;
00059         
00063         protected $_enabled;
00064 
00070         function __construct() {
00071 
00072                 $this->_ds = DS;
00073                 $this->_fld = ABS_CACHE;
00074                 $this->_prefix = 'cache_';
00075                 $this->_enabled = true;
00076         }
00077 
00084         protected function write($data) {
00085 
00086                 $filename = $this->getFilename();
00087 
00088                 if($fp = @fopen($filename, "xb")) {
00089                         if(flock($fp, LOCK_EX)) fwrite($fp, $data);
00090                         fclose($fp);
00091                         touch($filename, time());
00092                 }
00093 
00094         }
00095 
00101         protected function read() {
00102                 
00103                 return file_get_contents($this->getFilename());
00104 
00105         }
00106 
00112         protected function getFilename() {
00113 
00114                 return $this->_fld . $this->_ds . $this->_prefix . $this->_grp ."_". md5($this->_id);
00115 
00116         }
00117 
00123         protected function isCached() {
00124 
00125                 $filename = $this->getFilename();
00126                 if($this->_enabled && file_exists($filename) && time() < (filemtime($filename) + $this->_tc)) return true; 
00127                 else @unlink($filename);
00128                         
00129                 return false;
00130 
00131         }
00132 
00133 }
00134 
00169 class outputCache extends cache {
00170 
00174         protected $_buffer;
00175 
00183         function __construct(&$buffer, $enable = true) {
00184 
00185                 parent::__construct();
00186                 $this->_buffer = &$buffer;
00187                 $this->_enabled = $enable;
00188         }
00189 
00200         public function start($grp, $id, $tc) {
00201         
00202                 $this->_grp = $grp;
00203                 $this->_id = $id;
00204                 $this->_tc = $tc;
00205 
00206                 if($this->isCached()) {
00207                         $this->_buffer .= $this->read();
00208                         return false;
00209                 }
00210                 
00211                 return true;
00212 
00213         }
00214 
00223         public function stop($data) {
00224                 
00225                 if($this->_enabled) $this->write($data);
00226                 $this->_buffer .= $data;
00227 
00228         }
00229 
00230 }
00231 
00256 class dataCache extends cache {
00257 
00264         function __construct($enable = true) {
00265 
00266                 parent::__construct();
00267                 
00268                 $this->_enabled = $enable;
00269 
00270         }
00271         
00282         public function get($grp, $id, $tc) {
00283         
00284                 $this->_grp = $grp;
00285                 $this->_id = $id;
00286                 $this->_tc = $tc;
00287 
00288                 if($this->isCached()) return unserialize($this->read());
00289                 return false;
00290 
00291         }
00292         
00299         public function save($data) {
00300                 
00301                 if($this->_enabled) $this->write(serialize($data));
00302 
00303         }
00304 
00305 }
00306 
00307 ?>