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>
31 * @author Benoit Burnichon <bburnichon@gmail.com>
33 abstract class CacheProvider implements Cache, FlushableCache, ClearableCache, MultiOperationCache
35 const DOCTRINE_NAMESPACE_CACHEKEY = 'DoctrineNamespaceCacheKey[%s]';
38 * The namespace to prefix all cache ids with.
42 private $namespace = '';
45 * The namespace version.
49 private $namespaceVersion;
52 * Sets the namespace to prefix all cache ids with.
54 * @param string $namespace
58 public function setNamespace($namespace)
60 $this->namespace = (string) $namespace;
61 $this->namespaceVersion = null;
65 * Retrieves the namespace that prefixes all cache ids.
69 public function getNamespace()
71 return $this->namespace;
77 public function fetch($id)
79 return $this->doFetch($this->getNamespacedId($id));
85 public function fetchMultiple(array $keys)
91 // note: the array_combine() is in place to keep an association between our $keys and the $namespacedKeys
92 $namespacedKeys = array_combine($keys, array_map([$this, 'getNamespacedId'], $keys));
93 $items = $this->doFetchMultiple($namespacedKeys);
96 // no internal array function supports this sort of mapping: needs to be iterative
97 // this filters and combines keys in one pass
98 foreach ($namespacedKeys as $requestedKey => $namespacedKey) {
99 if (isset($items[$namespacedKey]) || array_key_exists($namespacedKey, $items)) {
100 $foundItems[$requestedKey] = $items[$namespacedKey];
110 public function saveMultiple(array $keysAndValues, $lifetime = 0)
112 $namespacedKeysAndValues = [];
113 foreach ($keysAndValues as $key => $value) {
114 $namespacedKeysAndValues[$this->getNamespacedId($key)] = $value;
117 return $this->doSaveMultiple($namespacedKeysAndValues, $lifetime);
123 public function contains($id)
125 return $this->doContains($this->getNamespacedId($id));
131 public function save($id, $data, $lifeTime = 0)
133 return $this->doSave($this->getNamespacedId($id), $data, $lifeTime);
139 public function deleteMultiple(array $keys)
141 return $this->doDeleteMultiple(array_map(array($this, 'getNamespacedId'), $keys));
147 public function delete($id)
149 return $this->doDelete($this->getNamespacedId($id));
155 public function getStats()
157 return $this->doGetStats();
163 public function flushAll()
165 return $this->doFlush();
171 public function deleteAll()
173 $namespaceCacheKey = $this->getNamespaceCacheKey();
174 $namespaceVersion = $this->getNamespaceVersion() + 1;
176 if ($this->doSave($namespaceCacheKey, $namespaceVersion)) {
177 $this->namespaceVersion = $namespaceVersion;
186 * Prefixes the passed id with the configured namespace value.
188 * @param string $id The id to namespace.
190 * @return string The namespaced id.
192 private function getNamespacedId(string $id) : string
194 $namespaceVersion = $this->getNamespaceVersion();
196 return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion);
200 * Returns the namespace cache key.
204 private function getNamespaceCacheKey() : string
206 return sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace);
210 * Returns the namespace version.
214 private function getNamespaceVersion() : int
216 if (null !== $this->namespaceVersion) {
217 return $this->namespaceVersion;
220 $namespaceCacheKey = $this->getNamespaceCacheKey();
221 $this->namespaceVersion = (int) $this->doFetch($namespaceCacheKey) ?: 1;
223 return $this->namespaceVersion;
227 * Default implementation of doFetchMultiple. Each driver that supports multi-get should owerwrite it.
229 * @param array $keys Array of keys to retrieve from cache
230 * @return array Array of values retrieved for the given keys.
232 protected function doFetchMultiple(array $keys)
236 foreach ($keys as $key) {
237 if (false !== ($item = $this->doFetch($key)) || $this->doContains($key)) {
238 $returnValues[$key] = $item;
242 return $returnValues;
246 * Fetches an entry from the cache.
248 * @param string $id The id of the cache entry to fetch.
250 * @return mixed|false The cached data or FALSE, if no cache entry exists for the given id.
252 abstract protected function doFetch($id);
255 * Tests if an entry exists in the cache.
257 * @param string $id The cache id of the entry to check for.
259 * @return bool TRUE if a cache entry exists for the given cache id, FALSE otherwise.
261 abstract protected function doContains($id);
264 * Default implementation of doSaveMultiple. Each driver that supports multi-put should override it.
266 * @param array $keysAndValues Array of keys and values to save in cache
267 * @param int $lifetime The lifetime. If != 0, sets a specific lifetime for these
268 * cache entries (0 => infinite lifeTime).
270 * @return bool TRUE if the operation was successful, FALSE if it wasn't.
272 protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
276 foreach ($keysAndValues as $key => $value) {
277 if (!$this->doSave($key, $value, $lifetime)) {
286 * Puts data into the cache.
288 * @param string $id The cache id.
289 * @param string $data The cache entry/data.
290 * @param int $lifeTime The lifetime. If != 0, sets a specific lifetime for this
291 * cache entry (0 => infinite lifeTime).
293 * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise.
295 abstract protected function doSave($id, $data, $lifeTime = 0);
298 * Default implementation of doDeleteMultiple. Each driver that supports multi-delete should override it.
300 * @param array $keys Array of keys to delete from cache
302 * @return bool TRUE if the operation was successful, FALSE if it wasn't
304 protected function doDeleteMultiple(array $keys)
308 foreach ($keys as $key) {
309 if (! $this->doDelete($key)) {
318 * Deletes a cache entry.
320 * @param string $id The cache id.
322 * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise.
324 abstract protected function doDelete($id);
327 * Flushes all cache entries.
329 * @return bool TRUE if the cache entries were successfully flushed, FALSE otherwise.
331 abstract protected function doFlush();
334 * Retrieves cached information from the data store.
338 * @return array|null An associative array with server's statistics if available, NULL otherwise.
340 abstract protected function doGetStats();