file.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: cache_2file_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 /**
00004  * File Storage engine for cache
00005  *
00006  *
00007  * PHP versions 4 and 5
00008  *
00009  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00010  * Copyright 2005-2008, Cake Software Foundation, Inc.
00011  *                              1785 E. Sahara Avenue, Suite 490-204
00012  *                              Las Vegas, Nevada 89104
00013  *
00014  * Licensed under The MIT License
00015  * Redistributions of files must retain the above copyright notice.
00016  *
00017  * @filesource
00018  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00019  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00020  * @package         cake
00021  * @subpackage      cake.cake.libs.cache
00022  * @since           CakePHP(tm) v 1.2.0.4933
00023  * @version         $Revision: 580 $
00024  * @modifiedby      $LastChangedBy: gwoo $
00025  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00026  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00027  */
00028 /**
00029  * File Storage engine for cache
00030  *
00031  * @todo use the File and Folder classes (if it's not a too big performance hit)
00032  * @package     cake
00033  * @subpackage  cake.cake.libs.cache
00034  */
00035 class FileEngine extends CacheEngine {
00036 /**
00037  * Instance of File class
00038  *
00039  * @var object
00040  * @access private
00041  */
00042     var $__File = null;
00043 /**
00044  * settings
00045  *      path = absolute path to cache directory, default => CACHE
00046  *      prefix = string prefix for filename, default => cake_
00047  *      lock = enable file locking on write, default => false
00048  *      serialize = serialize the data, default => true
00049  *
00050  * @var array
00051  * @see CacheEngine::__defaults
00052  * @access public
00053  */
00054     var $settings = array();
00055 /**
00056  * Set to true if FileEngine::init(); and FileEngine::__active(); do not fail.
00057  *
00058  * @var boolean
00059  * @access private
00060  */
00061     var $__active = false;
00062 /**
00063  * True unless FileEngine::__active(); fails
00064  *
00065  * @var boolean
00066  * @access private
00067  */
00068     var $__init = true;
00069 /**
00070  * Initialize the Cache Engine
00071  *
00072  * Called automatically by the cache frontend
00073  * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
00074  *
00075  * @param array $setting array of setting for the engine
00076  * @return boolean True if the engine has been successfully initialized, false if not
00077  * @access public
00078  */
00079     function init($settings = array()) {
00080         parent::init(array_merge(
00081             array(
00082                 'engine' => 'File', 'path' => CACHE, 'prefix'=> 'cake_', 'lock'=> false,
00083                 'serialize'=> true, 'isWindows' => false
00084             ),
00085             $settings
00086         ));
00087         if(!isset($this->__File)) {
00088             if (!class_exists('File')) {
00089                 uses('file');
00090             }
00091             $this->__File =& new File($this->settings['path'] . DS . 'cake');
00092         }
00093 
00094         if(substr(PHP_OS, 0, 3) == "WIN") {
00095             $this->settings['isWindows'] = true;
00096         }
00097 
00098         $this->settings['path'] = $this->__File->Folder->cd($this->settings['path']);
00099         if(empty($this->settings['path'])) {
00100             return false;
00101         }
00102         return $this->__active();
00103     }
00104 /**
00105  * Garbage collection. Permanently remove all expired and deleted data
00106  *
00107  * @return boolean True if garbage collection was succesful, false on failure
00108  * @access public
00109  */
00110     function gc() {
00111         return $this->clear(true);
00112     }
00113 /**
00114  * Write data for key into cache
00115  *
00116  * @param string $key Identifier for the data
00117  * @param mixed $data Data to be cached
00118  * @param mixed $duration How long to cache the data, in seconds
00119  * @return boolean True if the data was succesfully cached, false on failure
00120  * @access public
00121  */
00122     function write($key, &$data, $duration) {
00123         if ($data === '' || !$this->__init) {
00124             return false;
00125         }
00126 
00127         if($this->__setKey($key) === false) {
00128             return false;
00129         }
00130 
00131         if ($duration == null) {
00132             $duration = $this->settings['duration'];
00133         }
00134         $lineBreak = "\n";
00135 
00136         if ($this->settings['isWindows']) {
00137             $lineBreak = "\r\n";
00138         }
00139 
00140         if (!empty($this->settings['serialize'])) {
00141             if ($this->settings['isWindows']) {
00142                 $data = str_replace('\\', '\\\\\\\\', serialize($data));
00143             } else {
00144                 $data = serialize($data);
00145             }
00146         }
00147 
00148         if ($this->settings['lock']) {
00149             $this->__File->lock = true;
00150         }
00151         $expires = time() + $duration;
00152         $contents = $expires . $lineBreak . $data . $lineBreak;
00153         $success = $this->__File->write($contents);
00154         $this->__File->close();
00155         return $success;
00156     }
00157 /**
00158  * Read a key from the cache
00159  *
00160  * @param string $key Identifier for the data
00161  * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
00162  * @access public
00163  */
00164     function read($key) {
00165         if($this->__setKey($key) === false || !$this->__init) {
00166             return false;
00167         }
00168         if ($this->settings['lock']) {
00169             $this->__File->lock = true;
00170         }
00171         $cachetime = $this->__File->read(11);
00172 
00173         if ($cachetime !== false && intval($cachetime) < time()) {
00174             $this->__File->close();
00175             $this->__File->delete();
00176             return false;
00177         }
00178         $data = $this->__File->read(true);
00179 
00180         if ($data !== '' && !empty($this->settings['serialize'])) {
00181             if ($this->settings['isWindows']) {
00182                 $data = str_replace('\\\\\\\\', '\\', $data);
00183             }
00184             $data = unserialize($data);
00185         }
00186         $this->__File->close();
00187         return $data;
00188     }
00189 /**
00190  * Delete a key from the cache
00191  *
00192  * @param string $key Identifier for the data
00193  * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
00194  * @access public
00195  */
00196     function delete($key) {
00197         if($this->__setKey($key) === false || !$this->__init) {
00198             return false;
00199         }
00200         return $this->__File->delete();
00201     }
00202 /**
00203  * Delete all values from the cache
00204  *
00205  * @param boolean $check Optional - only delete expired cache items
00206  * @return boolean True if the cache was succesfully cleared, false otherwise
00207  * @access public
00208  */
00209     function clear($check) {
00210         if (!$this->__init) {
00211             return false;
00212         }
00213         $dir = dir($this->settings['path']);
00214         if ($check) {
00215             $now = time();
00216             $threshold = $now - $this->settings['duration'];
00217         }
00218         while (($entry = $dir->read()) !== false) {
00219             if($this->__setKey($entry) === false) {
00220                 continue;
00221             }
00222             if ($check) {
00223                 $mtime = $this->__File->lastChange();
00224 
00225                 if ($mtime === false || $mtime > $threshold) {
00226                     continue;
00227                 }
00228 
00229                 $expires = $this->__File->read(11);
00230                 $this->__File->close();
00231 
00232                 if ($expires > $now) {
00233                     continue;
00234                 }
00235             }
00236             $this->__File->delete();
00237         }
00238         $dir->close();
00239         return true;
00240     }
00241 /**
00242  * Get absolute file for a given key
00243  *
00244  * @param string $key The key
00245  * @return mixed Absolute cache file for the given key or false if erroneous
00246  * @access private
00247  */
00248     function __setKey($key) {
00249         $this->__File->Folder->cd($this->settings['path']);
00250         $this->__File->name = $key;
00251         if (!$this->__File->Folder->inPath($this->__File->pwd(), true)) {
00252             return false;
00253         }
00254     }
00255 /**
00256  * Determine is cache directory is writable
00257  *
00258  * @return boolean
00259  * @access private
00260  */
00261     function __active() {
00262         if (!$this->__active && $this->__init && !is_writable($this->settings['path'])) {
00263             $this->__init = false;
00264             trigger_error(sprintf(__('%s is not writable', true), $this->settings['path']), E_USER_WARNING);
00265         } else {
00266             $this->__active = true;
00267         }
00268         return true;
00269     }
00270 }
00271 ?>