Commit | Line | Data |
---|---|---|
ff49a0be S |
1 | <?php |
2 | namespace dns\system\cache; | |
3 | use dns\system\cache\builder\ICacheBuilder; | |
4 | use dns\system\cache\source\DiskCacheSource; | |
5 | use dns\system\SingletonFactory; | |
6 | ||
7 | define('CACHE_SOURCE_TYPE', 'disk'); | |
8 | ||
9 | /** | |
10 | * Manages transparent cache access. | |
11 | * | |
12 | * @author Alexander Ebert, Marcel Werk | |
13 | * @copyright 2001-2014 WoltLab GmbH | |
14 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
15 | * @package com.woltlab.wcf | |
16 | * @subpackage system.cache | |
17 | * @category Community Framework | |
18 | */ | |
19 | class CacheHandler extends SingletonFactory { | |
20 | /** | |
21 | * cache source object | |
22 | * @var \dns\system\cache\source\ICacheSource | |
23 | */ | |
24 | protected $cacheSource = null; | |
25 | ||
26 | /** | |
27 | * Creates a new CacheHandler object. | |
28 | */ | |
29 | protected function init() { | |
30 | // init cache source object | |
31 | try { | |
32 | $className = 'dns\system\cache\source\\'.ucfirst(CACHE_SOURCE_TYPE).'CacheSource'; | |
33 | if (class_exists($className)) { | |
34 | $this->cacheSource = new $className(); | |
35 | } | |
36 | else { | |
37 | // fallback to disk cache | |
38 | $this->cacheSource = new DiskCacheSource(); | |
39 | } | |
40 | } | |
41 | catch (\Exception $e) { | |
42 | if (CACHE_SOURCE_TYPE != 'disk') { | |
43 | // fallback to disk cache | |
44 | $this->cacheSource = new DiskCacheSource(); | |
45 | } | |
46 | else { | |
47 | throw $e; | |
48 | } | |
49 | } | |
50 | } | |
51 | ||
52 | /** | |
53 | * Flush cache for given resource. | |
54 | * | |
55 | * @param \dns\system\cache\builder\ICacheBuilder $cacheBuilder | |
56 | * @param array $parameters | |
57 | */ | |
58 | public function flush(ICacheBuilder $cacheBuilder, array $parameters) { | |
59 | $this->getCacheSource()->flush($this->getCacheName($cacheBuilder, $parameters), empty($parameters)); | |
60 | } | |
61 | ||
62 | /** | |
63 | * Flushes the entire cache. | |
64 | */ | |
65 | public function flushAll() { | |
66 | $this->getCacheSource()->flushAll(); | |
67 | } | |
68 | ||
69 | /** | |
70 | * Returns cached value for given resource, false if no cache exists. | |
71 | * | |
72 | * @param \dns\system\cache\builder\ICacheBuilder $cacheBuilder | |
73 | * @param array $parameters | |
74 | * @return mixed | |
75 | */ | |
76 | public function get(ICacheBuilder $cacheBuilder, array $parameters) { | |
77 | return $this->getCacheSource()->get($this->getCacheName($cacheBuilder, $parameters), $cacheBuilder->getMaxLifetime()); | |
78 | } | |
79 | ||
80 | /** | |
81 | * Caches a value for given resource, | |
82 | * | |
83 | * @param \dns\system\cache\builder\ICacheBuilder $cacheBuilder | |
84 | * @param array $parameters | |
85 | * @param array $data | |
86 | */ | |
87 | public function set(ICacheBuilder $cacheBuilder, array $parameters, array $data) { | |
88 | $this->getCacheSource()->set($this->getCacheName($cacheBuilder, $parameters), $data, $cacheBuilder->getMaxLifetime()); | |
89 | } | |
90 | ||
91 | /** | |
92 | * Returns cache index hash. | |
93 | * | |
94 | * @param array $parameters | |
95 | * @return string | |
96 | */ | |
97 | public function getCacheIndex(array $parameters) { | |
98 | return sha1(serialize($this->orderParameters($parameters))); | |
99 | } | |
100 | ||
101 | /** | |
102 | * Builds cache name. | |
103 | * | |
104 | * @param \dns\system\cache\builder\ICacheBuilder $cacheBuilder | |
105 | * @param array $parameters | |
106 | * @return string | |
107 | */ | |
108 | protected function getCacheName(ICacheBuilder $cacheBuilder, array $parameters = array()) { | |
109 | $className = explode('\\', get_class($cacheBuilder)); | |
110 | $cacheName = str_replace('CacheBuilder', '', array_pop($className)); | |
111 | if (!empty($parameters)) { | |
112 | $cacheName .= '-' . $this->getCacheIndex($parameters); | |
113 | } | |
114 | ||
115 | return ucfirst($cacheName); | |
116 | } | |
117 | ||
118 | /** | |
119 | * Returns the cache source object. | |
120 | * | |
121 | * @return \dns\system\cache\source\ICacheSource | |
122 | */ | |
123 | public function getCacheSource() { | |
124 | return $this->cacheSource; | |
125 | } | |
126 | ||
127 | /** | |
128 | * Unifys parameter order, numeric indizes will be discarded. | |
129 | * | |
130 | * @param array $parameters | |
131 | * @return array | |
132 | */ | |
133 | protected function orderParameters($parameters) { | |
134 | if (!empty($parameters)) { | |
135 | array_multisort($parameters); | |
136 | } | |
137 | ||
138 | return $parameters; | |
139 | } | |
140 | } |