cache.inc

  1. 8.0.x includes/cache.inc
  2. 8.0.x commands/core/drupal/cache.inc
  3. 6.x includes/cache.inc
  4. 7.x includes/cache.inc
  5. 7.x commands/core/drupal/cache.inc
  6. 5.x includes/cache.inc
  7. master commands/core/drupal/cache.inc
  8. master includes/cache.inc

Drush cache API

Provides a cache API for drush core and commands, forked from Drupal 7.

The default storage backend uses the plain text files to store serialized php objects, which can be extended or replaced by setting the cache-default-class option in drushrc.php.

Functions

Namesort descending Description
drush_cache_clear_all Expire data from the cache.
drush_cache_get Return data from the persistent cache.
drush_cache_get_bins Return drush cache bins and any bins added by hook_drush_flush_caches().
drush_cache_get_multiple Return data from the persistent cache when given an array of cache IDs.
drush_cache_set Store data in the persistent cache.
drush_get_cid Create a cache id from a given prefix, contexts, and additional parameters.
_drush_cache_get_object Get the cache object for a cache bin.
_drush_cache_is_empty Check if a cache bin is empty.

Constants

Namesort descending Description
DRUSH_CACHE_PERMANENT Indicates that the item should never be removed unless explicitly selected.
DRUSH_CACHE_TEMPORARY Indicates that the item should be removed at the next general cache wipe.

File

includes/cache.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Drush cache API
  5. *
  6. * Provides a cache API for drush core and commands, forked from Drupal 7.
  7. *
  8. * The default storage backend uses the plain text files to store serialized php
  9. * objects, which can be extended or replaced by setting the cache-default-class
  10. * option in drushrc.php.
  11. */
  12. use Drush\Log\LogLevel;
  13. /**
  14. * Indicates that the item should never be removed unless explicitly selected.
  15. *
  16. * The item may be removed using cache_clear_all() with a cache ID.
  17. */
  18. define('DRUSH_CACHE_PERMANENT', 0);
  19. /**
  20. * Indicates that the item should be removed at the next general cache wipe.
  21. */
  22. define('DRUSH_CACHE_TEMPORARY', -1);
  23. /**
  24. * Get the cache object for a cache bin.
  25. *
  26. * By default, this returns an instance of the \Drush\Cache\FileCache class.
  27. * Classes implementing \Drush\Cache\CacheInterface can register themselves
  28. * both as a default implementation and for specific bins.
  29. *
  30. * @see \Drush\Cache\CacheInterface
  31. *
  32. * @param string $bin
  33. * The cache bin for which the cache object should be returned.
  34. *
  35. * @return \Drush\Cache\CacheInterface
  36. * The cache object associated with the specified bin.
  37. */
  38. function _drush_cache_get_object($bin) {
  39. static $cache_objects;
  40. if (!isset($cache_objects[$bin])) {
  41. $class = drush_get_option('cache-class-' . $bin, NULL);
  42. if (!isset($class)) {
  43. $class = drush_get_option('cache-default-class', '\Drush\Cache\JSONCache');
  44. }
  45. $cache_objects[$bin] = new $class($bin);
  46. }
  47. return $cache_objects[$bin];
  48. }
  49. /**
  50. * Return data from the persistent cache.
  51. *
  52. * Data may be stored as either plain text or as serialized data.
  53. * _drush_cache_get() will automatically return unserialized
  54. * objects and arrays.
  55. *
  56. * @param string $cid
  57. * The cache ID of the data to retrieve.
  58. * @param string $bin
  59. * The cache bin to store the data in.
  60. *
  61. * @return
  62. * The cache or FALSE on failure.
  63. *
  64. */
  65. function drush_cache_get($cid, $bin = 'default') {
  66. $ret = _drush_cache_get_object($bin)->get($cid);
  67. $mess = $ret ? "HIT" : "MISS";
  68. drush_log(dt("Cache !mess cid: !cid", array('!mess' => $mess, '!cid' => $cid)), LogLevel::DEBUG);
  69. return $ret;
  70. }
  71. /**
  72. * Return data from the persistent cache when given an array of cache IDs.
  73. *
  74. * @param array $cids
  75. * An array of cache IDs for the data to retrieve. This is passed by
  76. * reference, and will have the IDs successfully returned from cache removed.
  77. * @param string $bin
  78. * The cache bin where the data is stored.
  79. *
  80. * @return
  81. * An array of the items successfully returned from cache indexed by cid.
  82. */
  83. function drush_cache_get_multiple(array &$cids, $bin = 'default') {
  84. return _drush_cache_get_object($bin)->getMultiple($cids);
  85. }
  86. /**
  87. * Store data in the persistent cache.
  88. *
  89. * @param string $cid
  90. * The cache ID of the data to store.
  91. *
  92. * @param $data
  93. * The data to store in the cache.
  94. * @param string $bin
  95. * The cache bin to store the data in.
  96. * @param $expire
  97. * One of the following values:
  98. * - DRUSH_CACHE_PERMANENT: Indicates that the item should never be removed
  99. * unless explicitly told to using drush_cache_clear_all() with a cache ID.
  100. * - DRUSH_CACHE_TEMPORARY: Indicates that the item should be removed at
  101. * the next general cache wipe.
  102. * - A Unix timestamp: Indicates that the item should be kept at least until
  103. * the given time, after which it behaves like DRUSH_CACHE_TEMPORARY.
  104. *
  105. * @return bool
  106. */
  107. function drush_cache_set($cid, $data, $bin = 'default', $expire = DRUSH_CACHE_PERMANENT) {
  108. $ret = _drush_cache_get_object($bin)->set($cid, $data, $expire);
  109. if ($ret) drush_log(dt("Cache SET cid: !cid", array('!cid' => $cid)), LogLevel::DEBUG);
  110. return $ret;
  111. }
  112. /**
  113. * Expire data from the cache.
  114. *
  115. * If called without arguments, expirable entries will be cleared from all known
  116. * cache bins.
  117. *
  118. * @param string $cid
  119. * If set, the cache ID to delete. Otherwise, all cache entries that can
  120. * expire are deleted.
  121. * @param string $bin
  122. * If set, the bin $bin to delete from. Mandatory
  123. * argument if $cid is set.
  124. * @param bool $wildcard
  125. * If $wildcard is TRUE, cache IDs starting with $cid are deleted in
  126. * addition to the exact cache ID specified by $cid. If $wildcard is
  127. * TRUE and $cid is '*' then the entire bin $bin is emptied.
  128. */
  129. function drush_cache_clear_all($cid = NULL, $bin = 'default', $wildcard = FALSE) {
  130. if (!isset($cid) && !isset($bin)) {
  131. foreach (drush_cache_get_bins() as $bin) {
  132. _drush_cache_get_object($bin)->clear();
  133. }
  134. return;
  135. }
  136. return _drush_cache_get_object($bin)->clear($cid, $wildcard);
  137. }
  138. /**
  139. * Check if a cache bin is empty.
  140. *
  141. * A cache bin is considered empty if it does not contain any valid data for any
  142. * cache ID.
  143. *
  144. * @param $bin
  145. * The cache bin to check.
  146. *
  147. * @return
  148. * TRUE if the cache bin specified is empty.
  149. */
  150. function _drush_cache_is_empty($bin) {
  151. return _drush_cache_get_object($bin)->isEmpty();
  152. }
  153. /**
  154. * Return drush cache bins and any bins added by hook_drush_flush_caches().
  155. */
  156. function drush_cache_get_bins() {
  157. $drush = array('default');
  158. return array_merge(drush_command_invoke_all('drush_flush_caches'), $drush);
  159. }
  160. /**
  161. * Create a cache id from a given prefix, contexts, and additional parameters.
  162. *
  163. * @param prefix
  164. * A human readable cid prefix that will not be hashed.
  165. * @param contexts
  166. * Array of drush contexts that will be used to build a unique hash.
  167. * @param params
  168. * Array of any addition parameters to be hashed.
  169. *
  170. * @return
  171. * A cache id string.
  172. */
  173. function drush_get_cid($prefix, $contexts = array(), $params = array()) {
  174. $cid = array();
  175. foreach ($contexts as $context) {
  176. $c = drush_get_context($context);
  177. if (!empty($c)) {
  178. $cid[] = is_scalar($c) ? $c : serialize($c);
  179. }
  180. }
  181. foreach ($params as $param) {
  182. $cid[] = $param;
  183. }
  184. return DRUSH_VERSION . '-' . $prefix . '-' . md5(implode("", $cid));
  185. }