3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\Common\Cache;
23 * Base class for cache provider implementations.
26 * @author Benjamin Eberlei <kontakt@beberlei.de>
27 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
28 * @author Jonathan Wage <jonwage@gmail.com>
29 * @author Roman Borschel <roman@code-factory.org>
30 * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
32 abstract class CacheProvider implements Cache, FlushableCache, ClearableCache, MultiGetCache, MultiPutCache
34 const DOCTRINE_NAMESPACE_CACHEKEY = 'DoctrineNamespaceCacheKey[%s]';
37 * The namespace to prefix all cache ids with.
41 private $namespace = '';
44 * The namespace version.
48 private $namespaceVersion;
51 * Sets the namespace to prefix all cache ids with.
53 * @param string $namespace
57 public function setNamespace($namespace)
59 $this->namespace = (string) $namespace;
60 $this->namespaceVersion = null;
64 * Retrieves the namespace that prefixes all cache ids.
68 public function getNamespace()
70 return $this->namespace;
76 public function fetch($id)
78 return $this->doFetch($this->getNamespacedId($id));
84 public function fetchMultiple(array $keys)
90 // note: the array_combine() is in place to keep an association between our $keys and the $namespacedKeys
91 $namespacedKeys = array_combine($keys, array_map(array($this, 'getNamespacedId'), $keys));
92 $items = $this->doFetchMultiple($namespacedKeys);
93 $foundItems = array();
95 // no internal array function supports this sort of mapping: needs to be iterative
96 // this filters and combines keys in one pass
97 foreach ($namespacedKeys as $requestedKey => $namespacedKey) {
98 if (isset($items[$namespacedKey]) || array_key_exists($namespacedKey, $items)) {
99 $foundItems[$requestedKey] = $items[$namespacedKey];
109 public function saveMultiple(array $keysAndValues, $lifetime = 0)
111 $namespacedKeysAndValues = array();
112 foreach ($keysAndValues as $key => $value) {
113 $namespacedKeysAndValues[$this->getNamespacedId($key)] = $value;
116 return $this->doSaveMultiple($namespacedKeysAndValues, $lifetime);
122 public function contains($id)
124 return $this->doContains($this->getNamespacedId($id));
130 public function save($id, $data, $lifeTime = 0)
132 return $this->doSave($this->getNamespacedId($id), $data, $lifeTime);
138 public function delete($id)
140 return $this->doDelete($this->getNamespacedId($id));
146 public function getStats()
148 return $this->doGetStats();
154 public function flushAll()
156 return $this->doFlush();
162 public function deleteAll()
164 $namespaceCacheKey = $this->getNamespaceCacheKey();
165 $namespaceVersion = $this->getNamespaceVersion() + 1;
167 if ($this->doSave($namespaceCacheKey, $namespaceVersion)) {
168 $this->namespaceVersion = $namespaceVersion;
177 * Prefixes the passed id with the configured namespace value.
179 * @param string $id The id to namespace.
181 * @return string The namespaced id.
183 private function getNamespacedId($id)
185 $namespaceVersion = $this->getNamespaceVersion();
187 return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion);
191 * Returns the namespace cache key.
195 private function getNamespaceCacheKey()
197 return sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace);
201 * Returns the namespace version.
205 private function getNamespaceVersion()
207 if (null !== $this->namespaceVersion) {
208 return $this->namespaceVersion;
211 $namespaceCacheKey = $this->getNamespaceCacheKey();
212 $this->namespaceVersion = $this->doFetch($namespaceCacheKey) ?: 1;
214 return $this->namespaceVersion;
218 * Default implementation of doFetchMultiple. Each driver that supports multi-get should owerwrite it.
220 * @param array $keys Array of keys to retrieve from cache
221 * @return array Array of values retrieved for the given keys.
223 protected function doFetchMultiple(array $keys)
225 $returnValues = array();
227 foreach ($keys as $key) {
228 if (false !== ($item = $this->doFetch($key)) || $this->doContains($key)) {
229 $returnValues[$key] = $item;
233 return $returnValues;
237 * Fetches an entry from the cache.
239 * @param string $id The id of the cache entry to fetch.
241 * @return mixed|false The cached data or FALSE, if no cache entry exists for the given id.
243 abstract protected function doFetch($id);
246 * Tests if an entry exists in the cache.
248 * @param string $id The cache id of the entry to check for.
250 * @return bool TRUE if a cache entry exists for the given cache id, FALSE otherwise.
252 abstract protected function doContains($id);
255 * Default implementation of doSaveMultiple. Each driver that supports multi-put should override it.
257 * @param array $keysAndValues Array of keys and values to save in cache
258 * @param int $lifetime The lifetime. If != 0, sets a specific lifetime for these
259 * cache entries (0 => infinite lifeTime).
261 * @return bool TRUE if the operation was successful, FALSE if it wasn't.
263 protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
267 foreach ($keysAndValues as $key => $value) {
268 if (!$this->doSave($key, $value, $lifetime)) {
277 * Puts data into the cache.
279 * @param string $id The cache id.
280 * @param string $data The cache entry/data.
281 * @param int $lifeTime The lifetime. If != 0, sets a specific lifetime for this
282 * cache entry (0 => infinite lifeTime).
284 * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise.
286 abstract protected function doSave($id, $data, $lifeTime = 0);
289 * Deletes a cache entry.
291 * @param string $id The cache id.
293 * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise.
295 abstract protected function doDelete($id);
298 * Flushes all cache entries.
300 * @return bool TRUE if the cache entries were successfully flushed, FALSE otherwise.
302 abstract protected function doFlush();
305 * Retrieves cached information from the data store.
309 * @return array|null An associative array with server's statistics if available, NULL otherwise.
311 abstract protected function doGetStats();