Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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