CacheProvider.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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, FlushableCache, ClearableCache, MultiGetCache, MultiPutCache
  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 integer|null
  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 fetchMultiple(array $keys)
  77. {
  78. if (empty($keys)) {
  79. return array();
  80. }
  81. // note: the array_combine() is in place to keep an association between our $keys and the $namespacedKeys
  82. $namespacedKeys = array_combine($keys, array_map(array($this, 'getNamespacedId'), $keys));
  83. $items = $this->doFetchMultiple($namespacedKeys);
  84. $foundItems = array();
  85. // no internal array function supports this sort of mapping: needs to be iterative
  86. // this filters and combines keys in one pass
  87. foreach ($namespacedKeys as $requestedKey => $namespacedKey) {
  88. if (isset($items[$namespacedKey]) || array_key_exists($namespacedKey, $items)) {
  89. $foundItems[$requestedKey] = $items[$namespacedKey];
  90. }
  91. }
  92. return $foundItems;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function saveMultiple(array $keysAndValues, $lifetime = 0)
  98. {
  99. $namespacedKeysAndValues = array();
  100. foreach ($keysAndValues as $key => $value) {
  101. $namespacedKeysAndValues[$this->getNamespacedId($key)] = $value;
  102. }
  103. return $this->doSaveMultiple($namespacedKeysAndValues, $lifetime);
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function contains($id)
  109. {
  110. return $this->doContains($this->getNamespacedId($id));
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function save($id, $data, $lifeTime = 0)
  116. {
  117. return $this->doSave($this->getNamespacedId($id), $data, $lifeTime);
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function delete($id)
  123. {
  124. return $this->doDelete($this->getNamespacedId($id));
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function getStats()
  130. {
  131. return $this->doGetStats();
  132. }
  133. /**
  134. * {@inheritDoc}
  135. */
  136. public function flushAll()
  137. {
  138. return $this->doFlush();
  139. }
  140. /**
  141. * {@inheritDoc}
  142. */
  143. public function deleteAll()
  144. {
  145. $namespaceCacheKey = $this->getNamespaceCacheKey();
  146. $namespaceVersion = $this->getNamespaceVersion() + 1;
  147. if ($this->doSave($namespaceCacheKey, $namespaceVersion)) {
  148. $this->namespaceVersion = $namespaceVersion;
  149. return true;
  150. }
  151. return false;
  152. }
  153. /**
  154. * Prefixes the passed id with the configured namespace value.
  155. *
  156. * @param string $id The id to namespace.
  157. *
  158. * @return string The namespaced id.
  159. */
  160. private function getNamespacedId($id)
  161. {
  162. $namespaceVersion = $this->getNamespaceVersion();
  163. return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion);
  164. }
  165. /**
  166. * Returns the namespace cache key.
  167. *
  168. * @return string
  169. */
  170. private function getNamespaceCacheKey()
  171. {
  172. return sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace);
  173. }
  174. /**
  175. * Returns the namespace version.
  176. *
  177. * @return integer
  178. */
  179. private function getNamespaceVersion()
  180. {
  181. if (null !== $this->namespaceVersion) {
  182. return $this->namespaceVersion;
  183. }
  184. $namespaceCacheKey = $this->getNamespaceCacheKey();
  185. $this->namespaceVersion = $this->doFetch($namespaceCacheKey) ?: 1;
  186. return $this->namespaceVersion;
  187. }
  188. /**
  189. * Default implementation of doFetchMultiple. Each driver that supports multi-get should owerwrite it.
  190. *
  191. * @param array $keys Array of keys to retrieve from cache
  192. * @return array Array of values retrieved for the given keys.
  193. */
  194. protected function doFetchMultiple(array $keys)
  195. {
  196. $returnValues = array();
  197. foreach ($keys as $key) {
  198. if (false !== ($item = $this->doFetch($key)) || $this->doContains($key)) {
  199. $returnValues[$key] = $item;
  200. }
  201. }
  202. return $returnValues;
  203. }
  204. /**
  205. * Fetches an entry from the cache.
  206. *
  207. * @param string $id The id of the cache entry to fetch.
  208. *
  209. * @return mixed|false The cached data or FALSE, if no cache entry exists for the given id.
  210. */
  211. abstract protected function doFetch($id);
  212. /**
  213. * Tests if an entry exists in the cache.
  214. *
  215. * @param string $id The cache id of the entry to check for.
  216. *
  217. * @return bool TRUE if a cache entry exists for the given cache id, FALSE otherwise.
  218. */
  219. abstract protected function doContains($id);
  220. /**
  221. * Default implementation of doSaveMultiple. Each driver that supports multi-put should override it.
  222. *
  223. * @param array $keysAndValues Array of keys and values to save in cache
  224. * @param int $lifetime The lifetime. If != 0, sets a specific lifetime for these
  225. * cache entries (0 => infinite lifeTime).
  226. *
  227. * @return bool TRUE if the operation was successful, FALSE if it wasn't.
  228. */
  229. protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
  230. {
  231. $success = true;
  232. foreach ($keysAndValues as $key => $value) {
  233. if (!$this->doSave($key, $value, $lifetime)) {
  234. $success = false;
  235. }
  236. }
  237. return $success;
  238. }
  239. /**
  240. * Puts data into the cache.
  241. *
  242. * @param string $id The cache id.
  243. * @param string $data The cache entry/data.
  244. * @param int $lifeTime The lifetime. If != 0, sets a specific lifetime for this
  245. * cache entry (0 => infinite lifeTime).
  246. *
  247. * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise.
  248. */
  249. abstract protected function doSave($id, $data, $lifeTime = 0);
  250. /**
  251. * Deletes a cache entry.
  252. *
  253. * @param string $id The cache id.
  254. *
  255. * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise.
  256. */
  257. abstract protected function doDelete($id);
  258. /**
  259. * Flushes all cache entries.
  260. *
  261. * @return bool TRUE if the cache entries were successfully flushed, FALSE otherwise.
  262. */
  263. abstract protected function doFlush();
  264. /**
  265. * Retrieves cached information from the data store.
  266. *
  267. * @since 2.2
  268. *
  269. * @return array|null An associative array with server's statistics if available, NULL otherwise.
  270. */
  271. abstract protected function doGetStats();
  272. }