1<?php 2 3/** 4* This class extends Cache_Lite and offers a cache system driven by a master file 5* 6* With this class, cache validity is only dependent of a given file. Cache files 7* are valid only if they are older than the master file. It's a perfect way for 8* caching templates results (if the template file is newer than the cache, cache 9* must be rebuild...) or for config classes... 10* There are some examples in the 'docs/examples' file 11* Technical choices are described in the 'docs/technical' file 12* 13* @package Cache_Lite 14* @author Fabien MARTY <fab@php.net> 15*/ 16 17require_once('Cache/Lite.php'); 18 19class Cache_Lite_File extends Cache_Lite 20{ 21 22 // --- Private properties --- 23 24 /** 25 * Complete path of the file used for controlling the cache lifetime 26 * 27 * @var string $_masterFile 28 */ 29 var $_masterFile = ''; 30 31 /** 32 * Masterfile mtime 33 * 34 * @var int $_masterFile_mtime 35 */ 36 var $_masterFile_mtime = 0; 37 38 // --- Public methods ---- 39 40 /** 41 * Constructor 42 * 43 * $options is an assoc. To have a look at availables options, 44 * see the constructor of the Cache_Lite class in 'Cache_Lite.php' 45 * 46 * Comparing to Cache_Lite constructor, there is another option : 47 * $options = array( 48 * (...) see Cache_Lite constructor 49 * 'masterFile' => complete path of the file used for controlling the cache lifetime(string) 50 * ); 51 * 52 * @param array $options options 53 * @access public 54 */ 55 function __construct($options = array(NULL)) 56 { 57 $options['lifetime'] = 0; 58 parent::__construct($options); 59 if (isset($options['masterFile'])) { 60 $this->_masterFile = $options['masterFile']; 61 } else { 62 return $this->raiseError('Cache_Lite_File : masterFile option must be set !'); 63 } 64 if (!($this->_masterFile_mtime = @filemtime($this->_masterFile))) { 65 return $this->raiseError('Cache_Lite_File : Unable to read masterFile : '.$this->_masterFile, -3); 66 } 67 } 68 69 /** 70 * PHP4 constructor for backwards compatibility with older code 71 * 72 * @param array $options Options 73 */ 74 function Cache_Lite_File($options = array(NULL)) 75 { 76 self::__construct($options); 77 } 78 79 /** 80 * Test if a cache is available and (if yes) return it 81 * 82 * @param string $id cache id 83 * @param string $group name of the cache group 84 * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested 85 * @return string data of the cache (else : false) 86 * @access public 87 */ 88 function get($id, $group = 'default', $doNotTestCacheValidity = false) 89 { 90 if ($data = parent::get($id, $group, true)) { 91 if ($filemtime = $this->lastModified()) { 92 if ($filemtime > $this->_masterFile_mtime) { 93 return $data; 94 } 95 } 96 } 97 return false; 98 } 99 100} 101