KumbiaPHP beta2-dev
Framework PHP en español
file_cache.php
Ir a la documentación de este archivo.
00001 <?php
00023 class FileCache extends Cache
00024 {
00032     protected function _getFilename($id, $group)
00033     {
00034         return 'cache_' . md5($id) . '.' . md5($group);
00035     }
00043         public function get($id, $group='default') 
00044     {
00045         $this->_id = $id;
00046         $this->_group = $group;
00047     
00048         $filename = APP_PATH . 'temp/cache/'.$this->_getFilename($id, $group);
00049                 if(file_exists($filename)){
00050             $fh = fopen($filename, 'r');
00051             
00052             $lifetime = trim(fgets($fh));
00053                         if($lifetime == 'undefined' || $lifetime >= time()) {
00054                 $data = stream_get_contents($fh);
00055             } else{
00056                 $data = null;
00057             }
00058             
00059             fclose($fh);
00060                         return $data;
00061                 }
00062         return null;
00063     }
00064     
00074         public function save($value, $lifetime=null, $id=false, $group='default')
00075     {
00076         if (! $id) {
00077             $id = $this->_id;
00078             $group = $this->_group;
00079         }
00080         
00081         if ($lifetime) {
00082             $lifetime = strtotime($lifetime);
00083         } else {
00084             $lifetime = 'undefined';
00085         }
00086     
00087         return file_put_contents(APP_PATH . 'temp/cache/'.$this->_getFilename($id, $group), "$lifetime\n$value");
00088     }
00095         public function clean($group=false)
00096     {
00097         $pattern = $group ? APP_PATH . 'temp/cache/' . '*.' . md5($group) : APP_PATH . 'temp/cache/*';
00098         foreach (glob($pattern) as $filename) {
00099             if(!unlink($filename)) {
00100                 return false;
00101             }
00102         }
00103                 return true;
00104     }
00112         public function remove($id, $group='default')
00113     {
00114         return unlink(APP_PATH . 'temp/cache/' . $this->_getFilename($id, $group));
00115     }
00116 }
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Enumeraciones