[44] | 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 | * @version $Id: File.php,v 1.3 2005/12/04 16:03:55 fab Exp $
|
---|
| 15 | * @author Fabien MARTY <fab@php.net>
|
---|
| 16 | */
|
---|
| 17 |
|
---|
| 18 | class net_php_pear_Cache_Lite_File extends net_php_pear_Cache_Lite
|
---|
| 19 | {
|
---|
| 20 |
|
---|
| 21 | // --- Private properties ---
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * Complete path of the file used for controlling the cache lifetime
|
---|
| 25 | *
|
---|
| 26 | * @var string $_masterFile
|
---|
| 27 | */
|
---|
| 28 | var $_masterFile = '';
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * Masterfile mtime
|
---|
| 32 | *
|
---|
| 33 | * @var int $_masterFile_mtime
|
---|
| 34 | */
|
---|
| 35 | var $_masterFile_mtime = 0;
|
---|
| 36 |
|
---|
| 37 | // --- Public methods ----
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * Constructor
|
---|
| 41 | *
|
---|
| 42 | * $options is an assoc. To have a look at availables options,
|
---|
| 43 | * see the constructor of the Cache_Lite class in 'Cache_Lite.php'
|
---|
| 44 | *
|
---|
| 45 | * Comparing to Cache_Lite constructor, there is another option :
|
---|
| 46 | * $options = array(
|
---|
| 47 | * (...) see Cache_Lite constructor
|
---|
| 48 | * 'masterFile' => complete path of the file used for controlling the cache lifetime(string)
|
---|
| 49 | * );
|
---|
| 50 | *
|
---|
| 51 | * @param array $options options
|
---|
| 52 | * @access public
|
---|
| 53 | */
|
---|
| 54 | function net_php_pear_Cache_Lite_File($options = array(NULL))
|
---|
| 55 | {
|
---|
| 56 | $options['lifetime'] = 0;
|
---|
| 57 | $this->net_php_pear_Cache_Lite($options);
|
---|
| 58 | if (isset($options['masterFile'])) {
|
---|
| 59 | $this->_masterFile = $options['masterFile'];
|
---|
| 60 | } else {
|
---|
| 61 | return $this->raiseError('Cache_Lite_File : masterFile option must be set !');
|
---|
| 62 | }
|
---|
| 63 | if (!($this->_masterFile_mtime = @filemtime($this->_masterFile))) {
|
---|
| 64 | return $this->raiseError('Cache_Lite_File : Unable to read masterFile : '.$this->_masterFile, -3);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * Test if a cache is available and (if yes) return it
|
---|
| 70 | *
|
---|
| 71 | * @param string $id cache id
|
---|
| 72 | * @param string $group name of the cache group
|
---|
| 73 | * @return string data of the cache (or false if no cache available)
|
---|
| 74 | * @access public
|
---|
| 75 | */
|
---|
| 76 | function get($id, $group = 'default')
|
---|
| 77 | {
|
---|
| 78 | if ($data = parent::get($id, $group, true)) {
|
---|
| 79 | if ($filemtime = $this->lastModified()) {
|
---|
| 80 | if ($filemtime > $this->_masterFile_mtime) {
|
---|
| 81 | return $data;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | return false;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | ?>
|
---|