2 namespace dns\system\cache\builder
;
3 use dns\system\cache\CacheHandler
;
4 use dns\system\SingletonFactory
;
7 * Default implementation for cache builders.
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
16 abstract class AbstractCacheBuilder
extends SingletonFactory
implements ICacheBuilder
{
18 * list of cache resources by index
21 protected $cache = array();
24 * maximum cache lifetime in seconds, '0' equals infinite
27 protected $maxLifetime = 0;
30 * @see \dns\system\cache\builder\ICacheBuilder::getData()
32 public function getData(array $parameters = array(), $arrayIndex = '') {
33 $index = CacheHandler
::getInstance()->getCacheIndex($parameters);
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);
42 CacheHandler
::getInstance()->set($this, $parameters, $this->cache
[$index]);
46 if (!empty($arrayIndex)) {
47 if (!isset($this->cache
[$index][$arrayIndex])) {
48 throw new \
Exception("array index '".$arrayIndex."' does not exist in cache resource");
51 return $this->cache
[$index][$arrayIndex];
54 return $this->cache
[$index];
58 * @see \dns\system\cache\builder\ICacheBuilder::getMaxLifetime()
60 public function getMaxLifetime() {
61 return $this->maxLifetime
;
65 * @see \dns\system\cache\builder\ICacheBuilder::reset()
67 public function reset(array $parameters = array()) {
68 CacheHandler
::getInstance()->flush($this, $parameters);
72 * Rebuilds cache for current resource.
74 * @param array $parameters
76 abstract protected function rebuild(array $parameters);