Chris@0
|
1 <?php
|
Chris@0
|
2 /*
|
Chris@0
|
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
Chris@0
|
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
Chris@0
|
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
Chris@0
|
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
Chris@0
|
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
Chris@0
|
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
Chris@0
|
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
Chris@0
|
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
Chris@0
|
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
Chris@0
|
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
Chris@0
|
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
Chris@0
|
14 *
|
Chris@0
|
15 * This software consists of voluntary contributions made by many individuals
|
Chris@0
|
16 * and is licensed under the MIT license. For more information, see
|
Chris@0
|
17 * <http://www.doctrine-project.org>.
|
Chris@0
|
18 */
|
Chris@0
|
19
|
Chris@0
|
20 namespace Doctrine\Common\Cache;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Base class for cache provider implementations.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @since 2.2
|
Chris@0
|
26 * @author Benjamin Eberlei <kontakt@beberlei.de>
|
Chris@0
|
27 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
Chris@0
|
28 * @author Jonathan Wage <jonwage@gmail.com>
|
Chris@0
|
29 * @author Roman Borschel <roman@code-factory.org>
|
Chris@0
|
30 * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
|
Chris@0
|
31 */
|
Chris@0
|
32 abstract class CacheProvider implements Cache, FlushableCache, ClearableCache, MultiGetCache, MultiPutCache
|
Chris@0
|
33 {
|
Chris@0
|
34 const DOCTRINE_NAMESPACE_CACHEKEY = 'DoctrineNamespaceCacheKey[%s]';
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * The namespace to prefix all cache ids with.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @var string
|
Chris@0
|
40 */
|
Chris@0
|
41 private $namespace = '';
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * The namespace version.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @var integer|null
|
Chris@0
|
47 */
|
Chris@0
|
48 private $namespaceVersion;
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * Sets the namespace to prefix all cache ids with.
|
Chris@0
|
52 *
|
Chris@0
|
53 * @param string $namespace
|
Chris@0
|
54 *
|
Chris@0
|
55 * @return void
|
Chris@0
|
56 */
|
Chris@0
|
57 public function setNamespace($namespace)
|
Chris@0
|
58 {
|
Chris@0
|
59 $this->namespace = (string) $namespace;
|
Chris@0
|
60 $this->namespaceVersion = null;
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 /**
|
Chris@0
|
64 * Retrieves the namespace that prefixes all cache ids.
|
Chris@0
|
65 *
|
Chris@0
|
66 * @return string
|
Chris@0
|
67 */
|
Chris@0
|
68 public function getNamespace()
|
Chris@0
|
69 {
|
Chris@0
|
70 return $this->namespace;
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * {@inheritdoc}
|
Chris@0
|
75 */
|
Chris@0
|
76 public function fetch($id)
|
Chris@0
|
77 {
|
Chris@0
|
78 return $this->doFetch($this->getNamespacedId($id));
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * {@inheritdoc}
|
Chris@0
|
83 */
|
Chris@0
|
84 public function fetchMultiple(array $keys)
|
Chris@0
|
85 {
|
Chris@0
|
86 if (empty($keys)) {
|
Chris@0
|
87 return array();
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 // note: the array_combine() is in place to keep an association between our $keys and the $namespacedKeys
|
Chris@0
|
91 $namespacedKeys = array_combine($keys, array_map(array($this, 'getNamespacedId'), $keys));
|
Chris@0
|
92 $items = $this->doFetchMultiple($namespacedKeys);
|
Chris@0
|
93 $foundItems = array();
|
Chris@0
|
94
|
Chris@0
|
95 // no internal array function supports this sort of mapping: needs to be iterative
|
Chris@0
|
96 // this filters and combines keys in one pass
|
Chris@0
|
97 foreach ($namespacedKeys as $requestedKey => $namespacedKey) {
|
Chris@0
|
98 if (isset($items[$namespacedKey]) || array_key_exists($namespacedKey, $items)) {
|
Chris@0
|
99 $foundItems[$requestedKey] = $items[$namespacedKey];
|
Chris@0
|
100 }
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 return $foundItems;
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 /**
|
Chris@0
|
107 * {@inheritdoc}
|
Chris@0
|
108 */
|
Chris@0
|
109 public function saveMultiple(array $keysAndValues, $lifetime = 0)
|
Chris@0
|
110 {
|
Chris@0
|
111 $namespacedKeysAndValues = array();
|
Chris@0
|
112 foreach ($keysAndValues as $key => $value) {
|
Chris@0
|
113 $namespacedKeysAndValues[$this->getNamespacedId($key)] = $value;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 return $this->doSaveMultiple($namespacedKeysAndValues, $lifetime);
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 /**
|
Chris@0
|
120 * {@inheritdoc}
|
Chris@0
|
121 */
|
Chris@0
|
122 public function contains($id)
|
Chris@0
|
123 {
|
Chris@0
|
124 return $this->doContains($this->getNamespacedId($id));
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 /**
|
Chris@0
|
128 * {@inheritdoc}
|
Chris@0
|
129 */
|
Chris@0
|
130 public function save($id, $data, $lifeTime = 0)
|
Chris@0
|
131 {
|
Chris@0
|
132 return $this->doSave($this->getNamespacedId($id), $data, $lifeTime);
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * {@inheritdoc}
|
Chris@0
|
137 */
|
Chris@0
|
138 public function delete($id)
|
Chris@0
|
139 {
|
Chris@0
|
140 return $this->doDelete($this->getNamespacedId($id));
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 /**
|
Chris@0
|
144 * {@inheritdoc}
|
Chris@0
|
145 */
|
Chris@0
|
146 public function getStats()
|
Chris@0
|
147 {
|
Chris@0
|
148 return $this->doGetStats();
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 /**
|
Chris@0
|
152 * {@inheritDoc}
|
Chris@0
|
153 */
|
Chris@0
|
154 public function flushAll()
|
Chris@0
|
155 {
|
Chris@0
|
156 return $this->doFlush();
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@0
|
159 /**
|
Chris@0
|
160 * {@inheritDoc}
|
Chris@0
|
161 */
|
Chris@0
|
162 public function deleteAll()
|
Chris@0
|
163 {
|
Chris@0
|
164 $namespaceCacheKey = $this->getNamespaceCacheKey();
|
Chris@0
|
165 $namespaceVersion = $this->getNamespaceVersion() + 1;
|
Chris@0
|
166
|
Chris@0
|
167 if ($this->doSave($namespaceCacheKey, $namespaceVersion)) {
|
Chris@0
|
168 $this->namespaceVersion = $namespaceVersion;
|
Chris@0
|
169
|
Chris@0
|
170 return true;
|
Chris@0
|
171 }
|
Chris@0
|
172
|
Chris@0
|
173 return false;
|
Chris@0
|
174 }
|
Chris@0
|
175
|
Chris@0
|
176 /**
|
Chris@0
|
177 * Prefixes the passed id with the configured namespace value.
|
Chris@0
|
178 *
|
Chris@0
|
179 * @param string $id The id to namespace.
|
Chris@0
|
180 *
|
Chris@0
|
181 * @return string The namespaced id.
|
Chris@0
|
182 */
|
Chris@0
|
183 private function getNamespacedId($id)
|
Chris@0
|
184 {
|
Chris@0
|
185 $namespaceVersion = $this->getNamespaceVersion();
|
Chris@0
|
186
|
Chris@0
|
187 return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion);
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 /**
|
Chris@0
|
191 * Returns the namespace cache key.
|
Chris@0
|
192 *
|
Chris@0
|
193 * @return string
|
Chris@0
|
194 */
|
Chris@0
|
195 private function getNamespaceCacheKey()
|
Chris@0
|
196 {
|
Chris@0
|
197 return sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace);
|
Chris@0
|
198 }
|
Chris@0
|
199
|
Chris@0
|
200 /**
|
Chris@0
|
201 * Returns the namespace version.
|
Chris@0
|
202 *
|
Chris@0
|
203 * @return integer
|
Chris@0
|
204 */
|
Chris@0
|
205 private function getNamespaceVersion()
|
Chris@0
|
206 {
|
Chris@0
|
207 if (null !== $this->namespaceVersion) {
|
Chris@0
|
208 return $this->namespaceVersion;
|
Chris@0
|
209 }
|
Chris@0
|
210
|
Chris@0
|
211 $namespaceCacheKey = $this->getNamespaceCacheKey();
|
Chris@0
|
212 $this->namespaceVersion = $this->doFetch($namespaceCacheKey) ?: 1;
|
Chris@0
|
213
|
Chris@0
|
214 return $this->namespaceVersion;
|
Chris@0
|
215 }
|
Chris@0
|
216
|
Chris@0
|
217 /**
|
Chris@0
|
218 * Default implementation of doFetchMultiple. Each driver that supports multi-get should owerwrite it.
|
Chris@0
|
219 *
|
Chris@0
|
220 * @param array $keys Array of keys to retrieve from cache
|
Chris@0
|
221 * @return array Array of values retrieved for the given keys.
|
Chris@0
|
222 */
|
Chris@0
|
223 protected function doFetchMultiple(array $keys)
|
Chris@0
|
224 {
|
Chris@0
|
225 $returnValues = array();
|
Chris@0
|
226
|
Chris@0
|
227 foreach ($keys as $key) {
|
Chris@0
|
228 if (false !== ($item = $this->doFetch($key)) || $this->doContains($key)) {
|
Chris@0
|
229 $returnValues[$key] = $item;
|
Chris@0
|
230 }
|
Chris@0
|
231 }
|
Chris@0
|
232
|
Chris@0
|
233 return $returnValues;
|
Chris@0
|
234 }
|
Chris@0
|
235
|
Chris@0
|
236 /**
|
Chris@0
|
237 * Fetches an entry from the cache.
|
Chris@0
|
238 *
|
Chris@0
|
239 * @param string $id The id of the cache entry to fetch.
|
Chris@0
|
240 *
|
Chris@0
|
241 * @return mixed|false The cached data or FALSE, if no cache entry exists for the given id.
|
Chris@0
|
242 */
|
Chris@0
|
243 abstract protected function doFetch($id);
|
Chris@0
|
244
|
Chris@0
|
245 /**
|
Chris@0
|
246 * Tests if an entry exists in the cache.
|
Chris@0
|
247 *
|
Chris@0
|
248 * @param string $id The cache id of the entry to check for.
|
Chris@0
|
249 *
|
Chris@0
|
250 * @return bool TRUE if a cache entry exists for the given cache id, FALSE otherwise.
|
Chris@0
|
251 */
|
Chris@0
|
252 abstract protected function doContains($id);
|
Chris@0
|
253
|
Chris@0
|
254 /**
|
Chris@0
|
255 * Default implementation of doSaveMultiple. Each driver that supports multi-put should override it.
|
Chris@0
|
256 *
|
Chris@0
|
257 * @param array $keysAndValues Array of keys and values to save in cache
|
Chris@0
|
258 * @param int $lifetime The lifetime. If != 0, sets a specific lifetime for these
|
Chris@0
|
259 * cache entries (0 => infinite lifeTime).
|
Chris@0
|
260 *
|
Chris@0
|
261 * @return bool TRUE if the operation was successful, FALSE if it wasn't.
|
Chris@0
|
262 */
|
Chris@0
|
263 protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
|
Chris@0
|
264 {
|
Chris@0
|
265 $success = true;
|
Chris@0
|
266
|
Chris@0
|
267 foreach ($keysAndValues as $key => $value) {
|
Chris@0
|
268 if (!$this->doSave($key, $value, $lifetime)) {
|
Chris@0
|
269 $success = false;
|
Chris@0
|
270 }
|
Chris@0
|
271 }
|
Chris@0
|
272
|
Chris@0
|
273 return $success;
|
Chris@0
|
274 }
|
Chris@0
|
275
|
Chris@0
|
276 /**
|
Chris@0
|
277 * Puts data into the cache.
|
Chris@0
|
278 *
|
Chris@0
|
279 * @param string $id The cache id.
|
Chris@0
|
280 * @param string $data The cache entry/data.
|
Chris@0
|
281 * @param int $lifeTime The lifetime. If != 0, sets a specific lifetime for this
|
Chris@0
|
282 * cache entry (0 => infinite lifeTime).
|
Chris@0
|
283 *
|
Chris@0
|
284 * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise.
|
Chris@0
|
285 */
|
Chris@0
|
286 abstract protected function doSave($id, $data, $lifeTime = 0);
|
Chris@0
|
287
|
Chris@0
|
288 /**
|
Chris@0
|
289 * Deletes a cache entry.
|
Chris@0
|
290 *
|
Chris@0
|
291 * @param string $id The cache id.
|
Chris@0
|
292 *
|
Chris@0
|
293 * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise.
|
Chris@0
|
294 */
|
Chris@0
|
295 abstract protected function doDelete($id);
|
Chris@0
|
296
|
Chris@0
|
297 /**
|
Chris@0
|
298 * Flushes all cache entries.
|
Chris@0
|
299 *
|
Chris@0
|
300 * @return bool TRUE if the cache entries were successfully flushed, FALSE otherwise.
|
Chris@0
|
301 */
|
Chris@0
|
302 abstract protected function doFlush();
|
Chris@0
|
303
|
Chris@0
|
304 /**
|
Chris@0
|
305 * Retrieves cached information from the data store.
|
Chris@0
|
306 *
|
Chris@0
|
307 * @since 2.2
|
Chris@0
|
308 *
|
Chris@0
|
309 * @return array|null An associative array with server's statistics if available, NULL otherwise.
|
Chris@0
|
310 */
|
Chris@0
|
311 abstract protected function doGetStats();
|
Chris@0
|
312 }
|