CacheProvider.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. * The namespace to prefix all cache ids with.
  35. *
  36. * @var string
  37. */
  38. private $namespace = '';
  39. /**
  40. * The namespace version.
  41. *
  42. * @var string
  43. */
  44. private $namespaceVersion;
  45. /**
  46. * Sets the namespace to prefix all cache ids with.
  47. *
  48. * @param string $namespace
  49. *
  50. * @return void
  51. */
  52. public function setNamespace($namespace)
  53. {
  54. $this->namespace = (string) $namespace;
  55. $this->namespaceVersion = null;
  56. }
  57. /**
  58. * Retrieves the namespace that prefixes all cache ids.
  59. *
  60. * @return string
  61. */
  62. public function getNamespace()
  63. {
  64. return $this->namespace;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function fetch($id)
  70. {
  71. return $this->doFetch($this->getNamespacedId($id));
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function contains($id)
  77. {
  78. return $this->doContains($this->getNamespacedId($id));
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function save($id, $data, $lifeTime = 0)
  84. {
  85. return $this->doSave($this->getNamespacedId($id), $data, $lifeTime);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function delete($id)
  91. {
  92. return $this->doDelete($this->getNamespacedId($id));
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function getStats()
  98. {
  99. return $this->doGetStats();
  100. }
  101. /**
  102. * Flushes all cache entries.
  103. *
  104. * @return boolean TRUE if the cache entries were successfully flushed, FALSE otherwise.
  105. */
  106. public function flushAll()
  107. {
  108. return $this->doFlush();
  109. }
  110. /**
  111. * Deletes all cache entries.
  112. *
  113. * @return boolean TRUE if the cache entries were successfully deleted, FALSE otherwise.
  114. */
  115. public function deleteAll()
  116. {
  117. $namespaceCacheKey = $this->getNamespaceCacheKey();
  118. $namespaceVersion = $this->getNamespaceVersion() + 1;
  119. $this->namespaceVersion = $namespaceVersion;
  120. return $this->doSave($namespaceCacheKey, $namespaceVersion);
  121. }
  122. /**
  123. * Prefixes the passed id with the configured namespace value.
  124. *
  125. * @param string $id The id to namespace.
  126. *
  127. * @return string The namespaced id.
  128. */
  129. private function getNamespacedId($id)
  130. {
  131. $namespaceVersion = $this->getNamespaceVersion();
  132. return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion);
  133. }
  134. /**
  135. * Returns the namespace cache key.
  136. *
  137. * @return string
  138. */
  139. private function getNamespaceCacheKey()
  140. {
  141. return sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace);
  142. }
  143. /**
  144. * Returns the namespace version.
  145. *
  146. * @return string
  147. */
  148. private function getNamespaceVersion()
  149. {
  150. if (null !== $this->namespaceVersion) {
  151. return $this->namespaceVersion;
  152. }
  153. $namespaceCacheKey = $this->getNamespaceCacheKey();
  154. $namespaceVersion = $this->doFetch($namespaceCacheKey);
  155. if (false === $namespaceVersion) {
  156. $namespaceVersion = 1;
  157. $this->doSave($namespaceCacheKey, $namespaceVersion);
  158. }
  159. $this->namespaceVersion = $namespaceVersion;
  160. return $this->namespaceVersion;
  161. }
  162. /**
  163. * Fetches an entry from the cache.
  164. *
  165. * @param string $id The id of the cache entry to fetch.
  166. *
  167. * @return string|bool The cached data or FALSE, if no cache entry exists for the given id.
  168. */
  169. abstract protected function doFetch($id);
  170. /**
  171. * Tests if an entry exists in the cache.
  172. *
  173. * @param string $id The cache id of the entry to check for.
  174. *
  175. * @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise.
  176. */
  177. abstract protected function doContains($id);
  178. /**
  179. * Puts data into the cache.
  180. *
  181. * @param string $id The cache id.
  182. * @param string $data The cache entry/data.
  183. * @param int $lifeTime The lifetime. If != 0, sets a specific lifetime for this
  184. * cache entry (0 => infinite lifeTime).
  185. *
  186. * @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise.
  187. */
  188. abstract protected function doSave($id, $data, $lifeTime = 0);
  189. /**
  190. * Deletes a cache entry.
  191. *
  192. * @param string $id The cache id.
  193. *
  194. * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
  195. */
  196. abstract protected function doDelete($id);
  197. /**
  198. * Flushes all cache entries.
  199. *
  200. * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
  201. */
  202. abstract protected function doFlush();
  203. /**
  204. * Retrieves cached information from the data store.
  205. *
  206. * @since 2.2
  207. *
  208. * @return array|null An associative array with server's statistics if available, NULL otherwise.
  209. */
  210. abstract protected function doGetStats();
  211. }