4 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 * This software consists of voluntary contributions made by many individuals
17 * and is licensed under the MIT license. For more information, see
18 * <http://www.doctrine-project.org>.
21 namespace Doctrine\Common\Cache;
24 * Base class for cache provider implementations.
27 * @author Benjamin Eberlei <kontakt@beberlei.de>
28 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
29 * @author Jonathan Wage <jonwage@gmail.com>
30 * @author Roman Borschel <roman@code-factory.org>
31 * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
33 abstract class CacheProvider implements Cache
35 const DOCTRINE_NAMESPACE_CACHEKEY = 'DoctrineNamespaceCacheKey[%s]';
38 * @var string The namespace to prefix all cache ids with
40 private $namespace = '';
43 * @var string The namespace version
45 private $namespaceVersion;
48 * Set the namespace to prefix all cache ids with.
50 * @param string $namespace
53 public function setNamespace($namespace)
55 $this->namespace = (string) $namespace;
59 * Retrieve the namespace that prefixes all cache ids.
63 public function getNamespace()
65 return $this->namespace;
71 public function fetch($id)
73 return $this->doFetch($this->getNamespacedId($id));
79 public function contains($id)
81 return $this->doContains($this->getNamespacedId($id));
87 public function save($id, $data, $lifeTime = 0)
89 return $this->doSave($this->getNamespacedId($id), $data, $lifeTime);
95 public function delete($id)
97 return $this->doDelete($this->getNamespacedId($id));
103 public function getStats()
105 return $this->doGetStats();
109 * Deletes all cache entries.
111 * @return boolean TRUE if the cache entries were successfully flushed, FALSE otherwise.
113 public function flushAll()
115 return $this->doFlush();
119 * Delete all cache entries.
121 * @return boolean TRUE if the cache entries were successfully deleted, FALSE otherwise.
123 public function deleteAll()
125 $namespaceCacheKey = $this->getNamespaceCacheKey();
126 $namespaceVersion = $this->getNamespaceVersion() + 1;
128 $this->namespaceVersion = $namespaceVersion;
130 return $this->doSave($namespaceCacheKey, $namespaceVersion);
134 * Prefix the passed id with the configured namespace value
136 * @param string $id The id to namespace
137 * @return string $id The namespaced id
139 private function getNamespacedId($id)
141 $namespaceVersion = $this->getNamespaceVersion();
143 return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion);
147 * Namespace cache key
149 * @return string $namespaceCacheKey
151 private function getNamespaceCacheKey()
153 return sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace);
159 * @return string $namespaceVersion
161 private function getNamespaceVersion()
163 if (null !== $this->namespaceVersion) {
164 return $this->namespaceVersion;
167 $namespaceCacheKey = $this->getNamespaceCacheKey();
168 $namespaceVersion = $this->doFetch($namespaceCacheKey);
170 if (false === $namespaceVersion) {
171 $namespaceVersion = 1;
173 $this->doSave($namespaceCacheKey, $namespaceVersion);
176 $this->namespaceVersion = $namespaceVersion;
178 return $this->namespaceVersion;
182 * Fetches an entry from the cache.
184 * @param string $id cache id The id of the cache entry to fetch.
185 * @return string The cached data or FALSE, if no cache entry exists for the given id.
187 abstract protected function doFetch($id);
190 * Test if an entry exists in the cache.
192 * @param string $id cache id The cache id of the entry to check for.
193 * @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise.
195 abstract protected function doContains($id);
198 * Puts data into the cache.
200 * @param string $id The cache id.
201 * @param string $data The cache entry/data.
202 * @param bool|int $lifeTime The lifetime. If != false, sets a specific lifetime for this
203 * cache entry (null => infinite lifeTime).
205 * @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise.
207 abstract protected function doSave($id, $data, $lifeTime = false);
210 * Deletes a cache entry.
212 * @param string $id cache id
213 * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
215 abstract protected function doDelete($id);
218 * Deletes all cache entries.
220 * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
222 abstract protected function doFlush();
225 * Retrieves cached information from data store
228 * @return array An associative array with server's statistics if available, NULL otherwise.
230 abstract protected function doGetStats();