1: <?php
2: 3: 4: 5: 6: 7:
8:
9: 10: 11: 12: 13: 14: 15:
16: abstract class Smarty_CacheResource
17: {
18: 19: 20: 21: 22:
23: public static $resources = array();
24:
25: 26: 27: 28: 29:
30: protected static $sysplugins = array(
31: 'file' => true,
32: );
33:
34: 35: 36: 37: 38: 39: 40: 41:
42: abstract public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template);
43:
44: 45: 46: 47: 48: 49: 50:
51: abstract public function populateTimestamp(Smarty_Template_Cached $cached);
52:
53: 54: 55: 56: 57: 58: 59: 60:
61: abstract public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null);
62:
63: 64: 65: 66: 67: 68: 69: 70:
71: abstract public function writeCachedContent(Smarty_Internal_Template $_template, $content);
72:
73: 74: 75: 76: 77: 78: 79:
80: public function getCachedContent(Smarty_Internal_Template $_template)
81: {
82: if ($_template->cached->handler->process($_template)) {
83: ob_start();
84: $_template->properties['unifunc']($_template);
85:
86: return ob_get_clean();
87: }
88:
89: return null;
90: }
91:
92: 93: 94: 95: 96: 97: 98: 99:
100: abstract public function clearAll(Smarty $smarty, $exp_time = null);
101:
102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112:
113: abstract public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time);
114:
115: 116: 117: 118: 119: 120:
121: public function locked(Smarty $smarty, Smarty_Template_Cached $cached)
122: {
123:
124: $start = microtime(true);
125: $hadLock = null;
126: while ($this->hasLock($smarty, $cached)) {
127: $hadLock = true;
128: if (microtime(true) - $start > $smarty->locking_timeout) {
129:
130: return false;
131: }
132: sleep(1);
133: }
134:
135: return $hadLock;
136: }
137:
138: 139: 140: 141: 142: 143: 144: 145:
146: public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
147: {
148:
149: return false;
150: }
151:
152: 153: 154: 155: 156: 157: 158: 159:
160: public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
161: {
162:
163: return true;
164: }
165:
166: 167: 168: 169: 170: 171: 172: 173:
174: public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
175: {
176:
177: return true;
178: }
179:
180: 181: 182: 183: 184: 185: 186: 187: 188:
189: public static function load(Smarty $smarty, $type = null)
190: {
191: if (!isset($type)) {
192: $type = $smarty->caching_type;
193: }
194:
195:
196: if (isset($smarty->_cacheresource_handlers[$type])) {
197: return $smarty->_cacheresource_handlers[$type];
198: }
199:
200:
201: if (isset($smarty->registered_cache_resources[$type])) {
202:
203: return $smarty->_cacheresource_handlers[$type] = $smarty->registered_cache_resources[$type];
204: }
205:
206: if (isset(self::$sysplugins[$type])) {
207: if (!isset(self::$resources[$type])) {
208: $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type);
209: self::$resources[$type] = new $cache_resource_class();
210: }
211:
212: return $smarty->_cacheresource_handlers[$type] = self::$resources[$type];
213: }
214:
215: $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type);
216: if ($smarty->loadPlugin($cache_resource_class)) {
217: if (!isset(self::$resources[$type])) {
218: self::$resources[$type] = new $cache_resource_class();
219: }
220:
221: return $smarty->_cacheresource_handlers[$type] = self::$resources[$type];
222: }
223:
224: throw new SmartyException("Unable to load cache resource '{$type}'");
225: }
226:
227: 228: 229: 230: 231:
232: public static function invalidLoadedCache(Smarty $smarty)
233: {
234: foreach ($smarty->template_objects as $tpl) {
235: if (isset($tpl->cached)) {
236: $tpl->cached->valid = false;
237: $tpl->cached->processed = false;
238: }
239: }
240: }
241: }
242:
243: 244: 245: 246: 247: 248: 249: 250:
251: class Smarty_Template_Cached
252: {
253: 254: 255: 256: 257:
258: public $filepath = false;
259:
260: 261: 262: 263: 264:
265: public $content = null;
266:
267: 268: 269: 270: 271:
272: public $timestamp = false;
273:
274: 275: 276: 277: 278:
279: public $exists = false;
280:
281: 282: 283: 284: 285:
286: public $valid = false;
287:
288: 289: 290: 291: 292:
293: public $processed = false;
294:
295: 296: 297: 298: 299:
300: public $handler = null;
301:
302: 303: 304: 305: 306:
307: public $compile_id = null;
308:
309: 310: 311: 312: 313:
314: public $cache_id = null;
315:
316: 317: 318: 319: 320:
321: public $lock_id = null;
322:
323: 324: 325: 326: 327:
328: public $is_locked = false;
329:
330: 331: 332: 333: 334:
335: public $source = null;
336:
337: 338: 339: 340: 341:
342: public function __construct(Smarty_Internal_Template $_template)
343: {
344: $this->compile_id = $_template->compile_id;
345: $this->cache_id = $_template->cache_id;
346: $this->source = $_template->source;
347: $_template->cached = $this;
348: $smarty = $_template->smarty;
349:
350:
351:
352:
353: $this->handler = $handler = Smarty_CacheResource::load($smarty);
354:
355:
356:
357:
358: if (!($_template->caching == Smarty::CACHING_LIFETIME_CURRENT || $_template->caching == Smarty::CACHING_LIFETIME_SAVED) || $_template->source->recompiled) {
359: $handler->populate($this, $_template);
360:
361: return;
362: }
363: while (true) {
364: while (true) {
365: $handler->populate($this, $_template);
366: if ($this->timestamp === false || $smarty->force_compile || $smarty->force_cache) {
367: $this->valid = false;
368: } else {
369: $this->valid = true;
370: }
371: if ($this->valid && $_template->caching == Smarty::CACHING_LIFETIME_CURRENT && $_template->cache_lifetime >= 0 && time() > ($this->timestamp + $_template->cache_lifetime)) {
372:
373: $this->valid = false;
374: }
375: if ($this->valid || !$_template->smarty->cache_locking) {
376: break;
377: }
378: if (!$this->handler->locked($_template->smarty, $this)) {
379: $this->handler->acquireLock($_template->smarty, $this);
380: break 2;
381: }
382: }
383: if ($this->valid) {
384: if (!$_template->smarty->cache_locking || $this->handler->locked($_template->smarty, $this) === null) {
385:
386: if ($smarty->debugging) {
387: Smarty_Internal_Debug::start_cache($_template);
388: }
389: if ($handler->process($_template, $this) === false) {
390: $this->valid = false;
391: } else {
392: $this->processed = true;
393: }
394: if ($smarty->debugging) {
395: Smarty_Internal_Debug::end_cache($_template);
396: }
397: } else {
398: continue;
399: }
400: } else {
401: return;
402: }
403: if ($this->valid && $_template->caching === Smarty::CACHING_LIFETIME_SAVED && $_template->properties['cache_lifetime'] >= 0 && (time() > ($_template->cached->timestamp + $_template->properties['cache_lifetime']))) {
404: $this->valid = false;
405: }
406: if (!$this->valid && $_template->smarty->cache_locking) {
407: $this->handler->acquireLock($_template->smarty, $this);
408:
409: return;
410: } else {
411: return;
412: }
413: }
414: }
415:
416: 417: 418: 419: 420: 421: 422: 423:
424: public function write(Smarty_Internal_Template $_template, $content)
425: {
426: if (!$_template->source->recompiled) {
427: if ($this->handler->writeCachedContent($_template, $content)) {
428: $this->content = null;
429: $this->timestamp = time();
430: $this->exists = true;
431: $this->valid = true;
432: if ($_template->smarty->cache_locking) {
433: $this->handler->releaseLock($_template->smarty, $this);
434: }
435:
436: return true;
437: }
438: }
439:
440: return false;
441: }
442: }
443: