CacheProvider.php 6.3KB

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