/vendor/doctrine-common/lib/Doctrine/Common/Cache/CacheProvider.php

https://github.com/israelnoguera/parejas · PHP · 189 lines · 58 code · 22 blank · 109 comment · 0 complexity · 6f97dd60e552b6692743f4e11a05e1e2 MD5 · raw file

  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. $idCacheKey = strtr($id, array('[' => '', ']' => ''));
  122. return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion);
  123. }
  124. /**
  125. * Fetches an entry from the cache.
  126. *
  127. * @param string $id cache id The id of the cache entry to fetch.
  128. * @return string The cached data or FALSE, if no cache entry exists for the given id.
  129. */
  130. abstract protected function doFetch($id);
  131. /**
  132. * Test if an entry exists in the cache.
  133. *
  134. * @param string $id cache id The cache id of the entry to check for.
  135. * @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise.
  136. */
  137. abstract protected function doContains($id);
  138. /**
  139. * Puts data into the cache.
  140. *
  141. * @param string $id The cache id.
  142. * @param string $data The cache entry/data.
  143. * @param int $lifeTime The lifetime. If != false, sets a specific lifetime for this cache entry (null => infinite lifeTime).
  144. * @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise.
  145. */
  146. abstract protected function doSave($id, $data, $lifeTime = false);
  147. /**
  148. * Deletes a cache entry.
  149. *
  150. * @param string $id cache id
  151. * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
  152. */
  153. abstract protected function doDelete($id);
  154. /**
  155. * Deletes all cache entries.
  156. *
  157. * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
  158. */
  159. abstract protected function doFlush();
  160. /**
  161. * Retrieves cached information from data store
  162. *
  163. * @since 2.2
  164. * @return array An associative array with server's statistics if available, NULL otherwise.
  165. */
  166. abstract protected function doGetStats();
  167. }