[345] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /* |
---|
| 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. |
---|
| 15 | * |
---|
| 16 | * This software consists of voluntary contributions made by many individuals |
---|
| 17 | * and is licensed under the LGPL. For more information, see |
---|
| 18 | * <http://www.doctrine-project.org>. |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | namespace Doctrine\Common\Cache; |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * Base class for cache provider implementations. |
---|
| 25 | * |
---|
| 26 | * @since 2.2 |
---|
| 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> |
---|
| 32 | */ |
---|
| 33 | abstract class CacheProvider implements Cache |
---|
| 34 | { |
---|
| 35 | const DOCTRINE_NAMESPACE_CACHEKEY = 'DoctrineNamespaceCacheKey[%s]'; |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * @var string The namespace to prefix all cache ids with |
---|
| 39 | */ |
---|
| 40 | private $namespace = ''; |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * Set the namespace to prefix all cache ids with. |
---|
| 44 | * |
---|
| 45 | * @param string $namespace |
---|
| 46 | * @return void |
---|
| 47 | */ |
---|
| 48 | public function setNamespace($namespace) |
---|
| 49 | { |
---|
| 50 | $this->namespace = (string) $namespace; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * Retrieve the namespace that prefixes all cache ids. |
---|
| 55 | * |
---|
| 56 | * @return string |
---|
| 57 | */ |
---|
| 58 | public function getNamespace() |
---|
| 59 | { |
---|
| 60 | return $this->namespace; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * {@inheritdoc} |
---|
| 65 | */ |
---|
| 66 | public function fetch($id) |
---|
| 67 | { |
---|
| 68 | return $this->doFetch($this->getNamespacedId($id)); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * {@inheritdoc} |
---|
| 73 | */ |
---|
| 74 | public function contains($id) |
---|
| 75 | { |
---|
| 76 | return $this->doContains($this->getNamespacedId($id)); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | /** |
---|
| 80 | * {@inheritdoc} |
---|
| 81 | */ |
---|
| 82 | public function save($id, $data, $lifeTime = 0) |
---|
| 83 | { |
---|
| 84 | return $this->doSave($this->getNamespacedId($id), $data, $lifeTime); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * {@inheritdoc} |
---|
| 89 | */ |
---|
| 90 | public function delete($id) |
---|
| 91 | { |
---|
| 92 | return $this->doDelete($this->getNamespacedId($id)); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | * {@inheritdoc} |
---|
| 97 | */ |
---|
| 98 | public function getStats() |
---|
| 99 | { |
---|
| 100 | return $this->doGetStats(); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * Deletes all cache entries. |
---|
| 105 | * |
---|
| 106 | * @return boolean TRUE if the cache entries were successfully flushed, FALSE otherwise. |
---|
| 107 | */ |
---|
| 108 | public function flushAll() |
---|
| 109 | { |
---|
| 110 | return $this->doFlush(); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * Delete all cache entries. |
---|
| 115 | * |
---|
| 116 | * @return boolean TRUE if the cache entries were successfully deleted, FALSE otherwise. |
---|
| 117 | */ |
---|
| 118 | public function deleteAll() |
---|
| 119 | { |
---|
| 120 | $namespaceCacheKey = sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace); |
---|
| 121 | $namespaceVersion = ($this->doContains($namespaceCacheKey)) ? $this->doFetch($namespaceCacheKey) : 1; |
---|
| 122 | |
---|
| 123 | return $this->doSave($namespaceCacheKey, $namespaceVersion + 1); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | /** |
---|
| 127 | * Prefix the passed id with the configured namespace value |
---|
| 128 | * |
---|
| 129 | * @param string $id The id to namespace |
---|
| 130 | * @return string $id The namespaced id |
---|
| 131 | */ |
---|
| 132 | private function getNamespacedId($id) |
---|
| 133 | { |
---|
| 134 | $namespaceCacheKey = sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace); |
---|
| 135 | $namespaceVersion = ($this->doContains($namespaceCacheKey)) ? $this->doFetch($namespaceCacheKey) : 1; |
---|
| 136 | |
---|
| 137 | return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | * Fetches an entry from the cache. |
---|
| 142 | * |
---|
| 143 | * @param string $id cache id The id of the cache entry to fetch. |
---|
| 144 | * @return string The cached data or FALSE, if no cache entry exists for the given id. |
---|
| 145 | */ |
---|
| 146 | abstract protected function doFetch($id); |
---|
| 147 | |
---|
| 148 | /** |
---|
| 149 | * Test if an entry exists in the cache. |
---|
| 150 | * |
---|
| 151 | * @param string $id cache id The cache id of the entry to check for. |
---|
| 152 | * @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise. |
---|
| 153 | */ |
---|
| 154 | abstract protected function doContains($id); |
---|
| 155 | |
---|
| 156 | /** |
---|
| 157 | * Puts data into the cache. |
---|
| 158 | * |
---|
| 159 | * @param string $id The cache id. |
---|
| 160 | * @param string $data The cache entry/data. |
---|
| 161 | * @param int $lifeTime The lifetime. If != false, sets a specific lifetime for this cache entry (null => infinite lifeTime). |
---|
| 162 | * @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise. |
---|
| 163 | */ |
---|
| 164 | abstract protected function doSave($id, $data, $lifeTime = false); |
---|
| 165 | |
---|
| 166 | /** |
---|
| 167 | * Deletes a cache entry. |
---|
| 168 | * |
---|
| 169 | * @param string $id cache id |
---|
| 170 | * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise. |
---|
| 171 | */ |
---|
| 172 | abstract protected function doDelete($id); |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | * Deletes all cache entries. |
---|
| 176 | * |
---|
| 177 | * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise. |
---|
| 178 | */ |
---|
| 179 | abstract protected function doFlush(); |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | * Retrieves cached information from data store |
---|
| 183 | * |
---|
| 184 | * @since 2.2 |
---|
| 185 | * @return array An associative array with server's statistics if available, NULL otherwise. |
---|
| 186 | */ |
---|
| 187 | abstract protected function doGetStats(); |
---|
| 188 | } |
---|