1 | <?php |
||
8 | class Cache |
||
9 | { |
||
10 | public $key = ''; // primary identifier for this item |
||
11 | public $ext = ''; // file ext for cache data, secondary identifier for this item |
||
12 | public $cache = ''; // cache file name |
||
13 | public $depends = array(); // array containing cache dependency information, |
||
14 | // used by _useCache to determine cache validity |
||
15 | |||
16 | public $_event = ''; // event to be triggered during useCache |
||
17 | public $_time; |
||
18 | public $_nocache = false; // if set to true, cache will not be used or stored |
||
19 | |||
20 | /** |
||
21 | * @param string $key primary identifier |
||
22 | * @param string $ext file extension |
||
23 | */ |
||
24 | public function __construct($key, $ext) |
||
30 | |||
31 | /** |
||
32 | * public method to determine whether the cache can be used |
||
33 | * |
||
34 | * to assist in centralisation of event triggering and calculation of cache statistics, |
||
35 | * don't override this function override _useCache() |
||
36 | * |
||
37 | * @param array $depends array of cache dependencies, support dependecies: |
||
38 | * 'age' => max age of the cache in seconds |
||
39 | * 'files' => cache must be younger than mtime of each file |
||
40 | * (nb. dependency passes if file doesn't exist) |
||
41 | * |
||
42 | * @return bool true if cache can be used, false otherwise |
||
43 | */ |
||
44 | public function useCache($depends = array()) |
||
55 | |||
56 | /** |
||
57 | * private method containing cache use decision logic |
||
58 | * |
||
59 | * this function processes the following keys in the depends array |
||
60 | * purge - force a purge on any non empty value |
||
61 | * age - expire cache if older than age (seconds) |
||
62 | * files - expire cache if any file in this array was updated more recently than the cache |
||
63 | * |
||
64 | * Note that this function needs to be public as it is used as callback for the event handler |
||
65 | * |
||
66 | * can be overridden |
||
67 | * |
||
68 | * @return bool see useCache() |
||
69 | */ |
||
70 | public function _useCache() |
||
98 | |||
99 | /** |
||
100 | * add dependencies to the depends array |
||
101 | * |
||
102 | * this method should only add dependencies, |
||
103 | * it should not remove any existing dependencies and |
||
104 | * it should only overwrite a dependency when the new value is more stringent than the old |
||
105 | */ |
||
106 | protected function _addDependencies() |
||
113 | |||
114 | /** |
||
115 | * retrieve the cached data |
||
116 | * |
||
117 | * @param bool $clean true to clean line endings, false to leave line endings alone |
||
118 | * @return string cache contents |
||
119 | */ |
||
120 | public function retrieveCache($clean = true) |
||
124 | |||
125 | /** |
||
126 | * cache $data |
||
127 | * |
||
128 | * @param string $data the data to be cached |
||
129 | * @return bool true on success, false otherwise |
||
130 | */ |
||
131 | public function storeCache($data) |
||
139 | |||
140 | /** |
||
141 | * remove any cached data associated with this cache instance |
||
142 | */ |
||
143 | public function removeCache() |
||
147 | |||
148 | /** |
||
149 | * Record cache hits statistics. |
||
150 | * (Only when debugging allowed, to reduce overhead.) |
||
151 | * |
||
152 | * @param bool $success result of this cache use attempt |
||
153 | * @return bool pass-thru $success value |
||
154 | */ |
||
155 | protected function _stats($success) |
||
193 | } |
||
194 |