CacheBackendInterface.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace Drupal\Core\Cache;
  3. /**
  4. * Defines an interface for cache implementations.
  5. *
  6. * All cache implementations have to implement this interface.
  7. * Drupal\Core\Cache\DatabaseBackend provides the default implementation, which
  8. * can be consulted as an example.
  9. *
  10. * The cache identifiers are case sensitive.
  11. *
  12. * @ingroup cache
  13. */
  14. interface CacheBackendInterface {
  15. /**
  16. * Indicates that the item should never be removed unless explicitly deleted.
  17. */
  18. const CACHE_PERMANENT = -1;
  19. /**
  20. * Returns data from the persistent cache.
  21. *
  22. * @param string $cid
  23. * The cache ID of the data to retrieve.
  24. * @param bool $allow_invalid
  25. * (optional) If TRUE, a cache item may be returned even if it is expired or
  26. * has been invalidated. Such items may sometimes be preferred, if the
  27. * alternative is recalculating the value stored in the cache, especially
  28. * if another concurrent request is already recalculating the same value.
  29. * The "valid" property of the returned object indicates whether the item is
  30. * valid or not. Defaults to FALSE.
  31. *
  32. * @return object|false
  33. * The cache item or FALSE on failure.
  34. *
  35. * @see \Drupal\Core\Cache\CacheBackendInterface::getMultiple()
  36. */
  37. public function get($cid, $allow_invalid = FALSE);
  38. /**
  39. * Returns data from the persistent cache when given an array of cache IDs.
  40. *
  41. * @param array $cids
  42. * An array of cache IDs for the data to retrieve. This is passed by
  43. * reference, and will have the IDs successfully returned from cache
  44. * removed.
  45. * @param bool $allow_invalid
  46. * (optional) If TRUE, cache items may be returned even if they have expired
  47. * or been invalidated. Such items may sometimes be preferred, if the
  48. * alternative is recalculating the value stored in the cache, especially
  49. * if another concurrent thread is already recalculating the same value. The
  50. * "valid" property of the returned objects indicates whether the items are
  51. * valid or not. Defaults to FALSE.
  52. *
  53. * @return array
  54. * An array of cache item objects indexed by cache ID.
  55. *
  56. * @see \Drupal\Core\Cache\CacheBackendInterface::get()
  57. */
  58. public function getMultiple(&$cids, $allow_invalid = FALSE);
  59. /**
  60. * Stores data in the persistent cache.
  61. *
  62. * Core cache implementations set the created time on cache item with
  63. * microtime(TRUE) rather than REQUEST_TIME_FLOAT, because the created time
  64. * of cache items should match when they are created, not when the request
  65. * started. Apart from being more accurate, this increases the chance an
  66. * item will legitimately be considered valid.
  67. *
  68. * @param string $cid
  69. * The cache ID of the data to store.
  70. * @param mixed $data
  71. * The data to store in the cache.
  72. * Some storage engines only allow objects up to a maximum of 1MB in size to
  73. * be stored by default. When caching large arrays or similar, take care to
  74. * ensure $data does not exceed this size.
  75. * @param int $expire
  76. * One of the following values:
  77. * - CacheBackendInterface::CACHE_PERMANENT: Indicates that the item should
  78. * not be removed unless it is deleted explicitly.
  79. * - A Unix timestamp: Indicates that the item will be considered invalid
  80. * after this time, i.e. it will not be returned by get() unless
  81. * $allow_invalid has been set to TRUE. When the item has expired, it may
  82. * be permanently deleted by the garbage collector at any time.
  83. * @param array $tags
  84. * An array of tags to be stored with the cache item. These should normally
  85. * identify objects used to build the cache item, which should trigger
  86. * cache invalidation when updated. For example if a cached item represents
  87. * a node, both the node ID and the author's user ID might be passed in as
  88. * tags. For example ['node:123', 'node:456', 'user:789'].
  89. *
  90. * @see \Drupal\Core\Cache\CacheBackendInterface::get()
  91. * @see \Drupal\Core\Cache\CacheBackendInterface::getMultiple()
  92. */
  93. public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []);
  94. /**
  95. * Store multiple items in the persistent cache.
  96. *
  97. * @param array $items
  98. * An array of cache items, keyed by cid. In the form:
  99. * @code
  100. * $items = array(
  101. * $cid => array(
  102. * // Required, will be automatically serialized if not a string.
  103. * 'data' => $data,
  104. * // Optional, defaults to CacheBackendInterface::CACHE_PERMANENT.
  105. * 'expire' => CacheBackendInterface::CACHE_PERMANENT,
  106. * // (optional) The cache tags for this item, see CacheBackendInterface::set().
  107. * 'tags' => array(),
  108. * ),
  109. * );
  110. * @endcode
  111. */
  112. public function setMultiple(array $items);
  113. /**
  114. * Deletes an item from the cache.
  115. *
  116. * If the cache item is being deleted because it is no longer "fresh", you may
  117. * consider using invalidate() instead. This allows callers to retrieve the
  118. * invalid item by calling get() with $allow_invalid set to TRUE. In some cases
  119. * an invalid item may be acceptable rather than having to rebuild the cache.
  120. *
  121. * @param string $cid
  122. * The cache ID to delete.
  123. *
  124. * @see \Drupal\Core\Cache\CacheBackendInterface::invalidate()
  125. * @see \Drupal\Core\Cache\CacheBackendInterface::deleteMultiple()
  126. * @see \Drupal\Core\Cache\CacheBackendInterface::deleteAll()
  127. */
  128. public function delete($cid);
  129. /**
  130. * Deletes multiple items from the cache.
  131. *
  132. * If the cache items are being deleted because they are no longer "fresh",
  133. * you may consider using invalidateMultiple() instead. This allows callers to
  134. * retrieve the invalid items by calling get() with $allow_invalid set to TRUE.
  135. * In some cases an invalid item may be acceptable rather than having to
  136. * rebuild the cache.
  137. *
  138. * @param array $cids
  139. * An array of cache IDs to delete.
  140. *
  141. * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple()
  142. * @see \Drupal\Core\Cache\CacheBackendInterface::delete()
  143. * @see \Drupal\Core\Cache\CacheBackendInterface::deleteAll()
  144. */
  145. public function deleteMultiple(array $cids);
  146. /**
  147. * Deletes all cache items in a bin.
  148. *
  149. * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateAll()
  150. * @see \Drupal\Core\Cache\CacheBackendInterface::delete()
  151. * @see \Drupal\Core\Cache\CacheBackendInterface::deleteMultiple()
  152. */
  153. public function deleteAll();
  154. /**
  155. * Marks a cache item as invalid.
  156. *
  157. * Invalid items may be returned in later calls to get(), if the $allow_invalid
  158. * argument is TRUE.
  159. *
  160. * @param string $cid
  161. * The cache ID to invalidate.
  162. *
  163. * @see \Drupal\Core\Cache\CacheBackendInterface::delete()
  164. * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple()
  165. * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateAll()
  166. */
  167. public function invalidate($cid);
  168. /**
  169. * Marks cache items as invalid.
  170. *
  171. * Invalid items may be returned in later calls to get(), if the $allow_invalid
  172. * argument is TRUE.
  173. *
  174. * @param string[] $cids
  175. * An array of cache IDs to invalidate.
  176. *
  177. * @see \Drupal\Core\Cache\CacheBackendInterface::deleteMultiple()
  178. * @see \Drupal\Core\Cache\CacheBackendInterface::invalidate()
  179. * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateAll()
  180. */
  181. public function invalidateMultiple(array $cids);
  182. /**
  183. * Marks all cache items as invalid.
  184. *
  185. * Invalid items may be returned in later calls to get(), if the $allow_invalid
  186. * argument is TRUE.
  187. *
  188. * @see \Drupal\Core\Cache\CacheBackendInterface::deleteAll()
  189. * @see \Drupal\Core\Cache\CacheBackendInterface::invalidate()
  190. * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple()
  191. */
  192. public function invalidateAll();
  193. /**
  194. * Performs garbage collection on a cache bin.
  195. *
  196. * The backend may choose to delete expired or invalidated items.
  197. */
  198. public function garbageCollection();
  199. /**
  200. * Remove a cache bin.
  201. */
  202. public function removeBin();
  203. }