file.php

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

Generated on Sun Nov 22 00:30:52 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7