add current dev version (WIP)
[GitHub/Stricted/Domain-Control-Panel.git] / lib / system / cache / builder / AbstractCacheBuilder.class.php
1 <?php
2 namespace dns\system\cache\builder;
3 use dns\system\cache\CacheHandler;
4 use dns\system\SingletonFactory;
5
6 /**
7 * Default implementation for cache builders.
8 *
9 * @author Alexander Ebert
10 * @copyright 2001-2014 WoltLab GmbH
11 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
12 * @package com.woltlab.wcf
13 * @subpackage system.cache.builder
14 * @category Community Framework
15 */
16 abstract class AbstractCacheBuilder extends SingletonFactory implements ICacheBuilder {
17 /**
18 * list of cache resources by index
19 * @var array<array>
20 */
21 protected $cache = array();
22
23 /**
24 * maximum cache lifetime in seconds, '0' equals infinite
25 * @var integer
26 */
27 protected $maxLifetime = 0;
28
29 /**
30 * @see \dns\system\cache\builder\ICacheBuilder::getData()
31 */
32 public function getData(array $parameters = array(), $arrayIndex = '') {
33 $index = CacheHandler::getInstance()->getCacheIndex($parameters);
34
35 if (!isset($this->cache[$index])) {
36 // fetch cache or rebuild if missing
37 $this->cache[$index] = CacheHandler::getInstance()->get($this, $parameters);
38 if ($this->cache[$index] === null) {
39 $this->cache[$index] = $this->rebuild($parameters);
40
41 // update cache
42 CacheHandler::getInstance()->set($this, $parameters, $this->cache[$index]);
43 }
44 }
45
46 if (!empty($arrayIndex)) {
47 if (!isset($this->cache[$index][$arrayIndex])) {
48 throw new \Exception("array index '".$arrayIndex."' does not exist in cache resource");
49 }
50
51 return $this->cache[$index][$arrayIndex];
52 }
53
54 return $this->cache[$index];
55 }
56
57 /**
58 * @see \dns\system\cache\builder\ICacheBuilder::getMaxLifetime()
59 */
60 public function getMaxLifetime() {
61 return $this->maxLifetime;
62 }
63
64 /**
65 * @see \dns\system\cache\builder\ICacheBuilder::reset()
66 */
67 public function reset(array $parameters = array()) {
68 CacheHandler::getInstance()->flush($this, $parameters);
69 }
70
71 /**
72 * Rebuilds cache for current resource.
73 *
74 * @param array $parameters
75 */
76 abstract protected function rebuild(array $parameters);
77 }