AbstractCache.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 driver implementations.
  22. *
  23. * @since 2.0
  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. */
  29. abstract class AbstractCache implements Cache
  30. {
  31. /** @var string The namespace to prefix all cache ids with */
  32. private $_namespace = '';
  33. /**
  34. * Set the namespace to prefix all cache ids with.
  35. *
  36. * @param string $namespace
  37. * @return void
  38. */
  39. public function setNamespace($namespace)
  40. {
  41. $this->_namespace = (string) $namespace;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function fetch($id)
  47. {
  48. return $this->_doFetch($this->_getNamespacedId($id));
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function contains($id)
  54. {
  55. return $this->_doContains($this->_getNamespacedId($id));
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function save($id, $data, $lifeTime = 0)
  61. {
  62. return $this->_doSave($this->_getNamespacedId($id), $data, $lifeTime);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function delete($id)
  68. {
  69. $id = $this->_getNamespacedId($id);
  70. if (strpos($id, '*') !== false) {
  71. return $this->deleteByRegex('/' . str_replace('*', '.*', $id) . '/');
  72. }
  73. return $this->_doDelete($id);
  74. }
  75. /**
  76. * Delete all cache entries.
  77. *
  78. * @return array $deleted Array of the deleted cache ids
  79. */
  80. public function deleteAll()
  81. {
  82. $ids = $this->getIds();
  83. foreach ($ids as $id) {
  84. $this->delete($id);
  85. }
  86. return $ids;
  87. }
  88. /**
  89. * Delete cache entries where the id matches a PHP regular expressions
  90. *
  91. * @param string $regex
  92. * @return array $deleted Array of the deleted cache ids
  93. */
  94. public function deleteByRegex($regex)
  95. {
  96. $deleted = array();
  97. $ids = $this->getIds();
  98. foreach ($ids as $id) {
  99. if (preg_match($regex, $id)) {
  100. $this->delete($id);
  101. $deleted[] = $id;
  102. }
  103. }
  104. return $deleted;
  105. }
  106. /**
  107. * Delete cache entries where the id has the passed prefix
  108. *
  109. * @param string $prefix
  110. * @return array $deleted Array of the deleted cache ids
  111. */
  112. public function deleteByPrefix($prefix)
  113. {
  114. $deleted = array();
  115. $prefix = $this->_getNamespacedId($prefix);
  116. $ids = $this->getIds();
  117. foreach ($ids as $id) {
  118. if (strpos($id, $prefix) === 0) {
  119. $this->delete($id);
  120. $deleted[] = $id;
  121. }
  122. }
  123. return $deleted;
  124. }
  125. /**
  126. * Delete cache entries where the id has the passed suffix
  127. *
  128. * @param string $suffix
  129. * @return array $deleted Array of the deleted cache ids
  130. */
  131. public function deleteBySuffix($suffix)
  132. {
  133. $deleted = array();
  134. $ids = $this->getIds();
  135. foreach ($ids as $id) {
  136. if (substr($id, -1 * strlen($suffix)) === $suffix) {
  137. $this->delete($id);
  138. $deleted[] = $id;
  139. }
  140. }
  141. return $deleted;
  142. }
  143. /**
  144. * Prefix the passed id with the configured namespace value
  145. *
  146. * @param string $id The id to namespace
  147. * @return string $id The namespaced id
  148. */
  149. private function _getNamespacedId($id)
  150. {
  151. return $this->_namespace . $id;
  152. }
  153. /**
  154. * Fetches an entry from the cache.
  155. *
  156. * @param string $id cache id The id of the cache entry to fetch.
  157. * @return string The cached data or FALSE, if no cache entry exists for the given id.
  158. */
  159. abstract protected function _doFetch($id);
  160. /**
  161. * Test if an entry exists in the cache.
  162. *
  163. * @param string $id cache id The cache id of the entry to check for.
  164. * @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise.
  165. */
  166. abstract protected function _doContains($id);
  167. /**
  168. * Puts data into the cache.
  169. *
  170. * @param string $id The cache id.
  171. * @param string $data The cache entry/data.
  172. * @param int $lifeTime The lifetime. If != false, sets a specific lifetime for this cache entry (null => infinite lifeTime).
  173. * @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise.
  174. */
  175. abstract protected function _doSave($id, $data, $lifeTime = false);
  176. /**
  177. * Deletes a cache entry.
  178. *
  179. * @param string $id cache id
  180. * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
  181. */
  182. abstract protected function _doDelete($id);
  183. /**
  184. * Get an array of all the cache ids stored
  185. *
  186. * @return array $ids
  187. */
  188. abstract public function getIds();
  189. }