CacheProvider.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 LGPL. 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. * Set the namespace to prefix all cache ids with.
  39. *
  40. * @param string $namespace
  41. * @return void
  42. */
  43. public function setNamespace($namespace)
  44. {
  45. $this->namespace = (string) $namespace;
  46. }
  47. /**
  48. * Retrieve the namespace that prefixes all cache ids.
  49. *
  50. * @return string
  51. */
  52. public function getNamespace()
  53. {
  54. return $this->namespace;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function fetch($id)
  60. {
  61. return $this->doFetch($this->getNamespacedId($id));
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function contains($id)
  67. {
  68. return $this->doContains($this->getNamespacedId($id));
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function save($id, $data, $lifeTime = 0)
  74. {
  75. return $this->doSave($this->getNamespacedId($id), $data, $lifeTime);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function delete($id)
  81. {
  82. return $this->doDelete($this->getNamespacedId($id));
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function getStats()
  88. {
  89. return $this->doGetStats();
  90. }
  91. /**
  92. * Deletes all cache entries.
  93. *
  94. * @return boolean TRUE if the cache entries were successfully flushed, FALSE otherwise.
  95. */
  96. public function flushAll()
  97. {
  98. return $this->doFlush();
  99. }
  100. /**
  101. * Delete all cache entries.
  102. *
  103. * @return boolean TRUE if the cache entries were successfully deleted, FALSE otherwise.
  104. */
  105. public function deleteAll()
  106. {
  107. $namespaceCacheKey = sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace);
  108. $namespaceVersion = ($this->doContains($namespaceCacheKey)) ? $this->doFetch($namespaceCacheKey) : 1;
  109. return $this->doSave($namespaceCacheKey, $namespaceVersion + 1);
  110. }
  111. /**
  112. * Prefix the passed id with the configured namespace value
  113. *
  114. * @param string $id The id to namespace
  115. * @return string $id The namespaced id
  116. */
  117. private function getNamespacedId($id)
  118. {
  119. $namespaceCacheKey = sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace);
  120. $namespaceVersion = ($this->doContains($namespaceCacheKey)) ? $this->doFetch($namespaceCacheKey) : 1;
  121. return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion);
  122. }
  123. /**
  124. * Fetches an entry from the cache.
  125. *
  126. * @param string $id cache id The id of the cache entry to fetch.
  127. * @return string The cached data or FALSE, if no cache entry exists for the given id.
  128. */
  129. abstract protected function doFetch($id);
  130. /**
  131. * Test if an entry exists in the cache.
  132. *
  133. * @param string $id cache id The cache id of the entry to check for.
  134. * @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise.
  135. */
  136. abstract protected function doContains($id);
  137. /**
  138. * Puts data into the cache.
  139. *
  140. * @param string $id The cache id.
  141. * @param string $data The cache entry/data.
  142. * @param int $lifeTime The lifetime. If != false, sets a specific lifetime for this cache entry (null => infinite lifeTime).
  143. * @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise.
  144. */
  145. abstract protected function doSave($id, $data, $lifeTime = false);
  146. /**
  147. * Deletes a cache entry.
  148. *
  149. * @param string $id cache id
  150. * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
  151. */
  152. abstract protected function doDelete($id);
  153. /**
  154. * Deletes all cache entries.
  155. *
  156. * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
  157. */
  158. abstract protected function doFlush();
  159. /**
  160. * Retrieves cached information from data store
  161. *
  162. * @since 2.2
  163. * @return array An associative array with server's statistics if available, NULL otherwise.
  164. */
  165. abstract protected function doGetStats();
  166. }