cache.drush.inc

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

Functions

Namesort descending Description
cache_cache_command_clear_complete Command argument complete callback.
cache_drush_command Implementation of hook_drush_command().
drush_cache_clear_block Clear the cache of the block output.
drush_cache_clear_both Clear caches internal to Drush core and Drupal.
drush_cache_clear_css_js
drush_cache_clear_drush Clear caches internal to drush core.
drush_cache_clear_theme_registry
drush_cache_clear_types All types of caches available for clearing. Contrib commands can alter in their own.
drush_cache_command_clear Command callback for drush cache-clear.
drush_cache_command_get Print an object returned from the cache.
drush_cache_command_set Set an object in the cache.

File

commands/core/cache.drush.inc
View source
  1. <?php
  2. /**
  3. * Implementation of hook_drush_command().
  4. */
  5. function cache_drush_command() {
  6. $items = array();
  7. // We specify command callbacks here because the defaults would collide with
  8. // the drush cache api functions.
  9. $items['cache-get'] = array(
  10. 'description' => 'Fetch a cached object and display it.',
  11. 'examples' => array(
  12. 'drush cache-get schema' => 'Display the data for the cache id "schema" from the "cache" bin.',
  13. 'drush cache-get update_available_releases update' => 'Display the data for the cache id "update_available_releases" from the "update" bin.',
  14. ),
  15. 'arguments' => array(
  16. 'cid' => 'The id of the object to fetch.',
  17. 'bin' => 'Optional. The cache bin to fetch from.',
  18. ),
  19. 'required-arguments' => 1,
  20. 'callback' => 'drush_cache_command_get',
  21. 'outputformat' => array(
  22. 'default' => 'print-r',
  23. 'pipe-format' => 'var_export',
  24. 'output-data-type' => TRUE,
  25. ),
  26. 'aliases' => array('cg'),
  27. );
  28. $items['cache-clear'] = array(
  29. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  30. 'description' => 'Clear a specific cache, or all drupal caches.',
  31. 'arguments' => array(
  32. 'type' => 'The particular cache to clear. Omit this argument to choose from available caches.',
  33. ),
  34. 'callback' => 'drush_cache_command_clear',
  35. 'aliases' => array('cc'),
  36. );
  37. $items['cache-set'] = array(
  38. 'description' => 'Cache an object expressed in JSON or var_export() format.',
  39. 'arguments' => array(
  40. 'cid' => 'The id of the object to set.',
  41. 'data' => 'The object to set in the cache. Use \'-\' to read the object from STDIN.',
  42. 'bin' => 'Optional. The cache bin to store the object in.',
  43. 'expire' => 'Optional. CACHE_PERMANENT, CACHE_TEMPORARY, or a Unix timestamp.',
  44. ),
  45. 'required-arguments' => 2,
  46. 'options' => array(
  47. // Note that this is not an outputformat option.
  48. 'format' => 'Format to parse the object. Use "string" for string (default), and "json" for JSON.',
  49. 'cache-get' => 'If the object is the result a previous fetch from the cache, only store the value in the "data" property of the object in the cache.',
  50. ),
  51. 'callback' => 'drush_cache_command_set',
  52. 'aliases' => array('cs'),
  53. );
  54. return $items;
  55. }
  56. /**
  57. * Command argument complete callback.
  58. *
  59. * @return
  60. * Array of clear types.
  61. */
  62. function cache_cache_command_clear_complete() {
  63. return array('values' => array_keys(drush_cache_clear_types(TRUE)));
  64. }
  65. /**
  66. * Command callback for drush cache-clear.
  67. */
  68. function drush_cache_command_clear($type = NULL) {
  69. $types = drush_cache_clear_types(drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL));
  70. // Check if the provided type ($type) is a valid cache type.
  71. if ($type && !key_exists($type, $types)) {
  72. // If we haven't done a full bootstrap, provide a more
  73. // specific message with instructions to the user on
  74. // bootstrapping a Drupal site for more options.
  75. if (!drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  76. $all_types = drush_cache_clear_types(TRUE);
  77. if (key_exists($type, $all_types)) {
  78. return drush_set_error(dt("'!type' cache requires a working Drupal site to operate on. Use the --root and --uri options, or a site @alias, or cd to a directory containing a Drupal settings.php file.", array('!type' => $type)));
  79. }
  80. else {
  81. return drush_set_error(dt("'!type' cache is not a valid cache type. There may be more cache types available if you select a working Drupal site.", array('!type' => $type)));
  82. }
  83. }
  84. return drush_set_error(dt("'!type' cache is not a valid cache type.", array('!type' => $type)));
  85. }
  86. if ($type) {
  87. drush_op($types[$type]);
  88. if ($type == 'all' && !drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  89. $type = 'drush';
  90. }
  91. }
  92. else {
  93. // Don't offer 'all' unless Drush has bootstrapped the Drupal site
  94. if (!drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  95. unset($types['all']);
  96. }
  97. $type = drush_choice($types, 'Enter a number to choose which cache to clear.', '!key');
  98. if ($type !== FALSE) {
  99. drush_op($types[$type]);
  100. }
  101. }
  102. if ($type !== FALSE) {
  103. drush_log(dt("'!name' cache was cleared.", array('!name' => $type)), 'success');
  104. }
  105. }
  106. /**
  107. * Print an object returned from the cache.
  108. *
  109. * @param $cid
  110. * The cache ID of the object to fetch.
  111. * @param $bin
  112. * Optional parameter to specify a specific bin to fetch from.
  113. */
  114. function drush_cache_command_get($cid = NULL, $bin = NULL) {
  115. if (!$cid) {
  116. return drush_set_error('DRUSH_CACHE_NOT_SPECIFIED', dt('You must specify a cache id to fetch.'));
  117. }
  118. if (!$bin) {
  119. $bin = 'cache';
  120. }
  121. $result = cache_get($cid, $bin);
  122. if (empty($result)) {
  123. return drush_set_error('DRUSH_CACHE_OBJECT_NOT_FOUND', dt('The !cid object in the !bin cache bin was not found.', array('!cid' => $cid, '!bin' => $bin)));
  124. }
  125. return $result;
  126. }
  127. /**
  128. * Set an object in the cache.
  129. *
  130. * @param $cid
  131. * The cache ID of the object to fetch.
  132. * @param $data
  133. * The data to save to the cache, or '-' to read from STDIN.
  134. * @param $bin
  135. * Optional parameter to specify a specific bin to fetch from.
  136. * @param $expire
  137. * Optional parameter to specify the expiry of the cached object.
  138. */
  139. function drush_cache_command_set($cid = NULL, $data = '', $bin = NULL, $expire = CACHE_PERMANENT) {
  140. if (!$bin) {
  141. $bin = 'cache';
  142. }
  143. if ($data == '-') {
  144. $data = stream_get_contents(STDIN);
  145. }
  146. // Convert the "expire" argument to a valid value for Drupal's cache_set().
  147. if ($expire == 'CACHE_TEMPORARY') {
  148. $expire = CACHE_TEMPORARY;
  149. }
  150. if ($expire == 'CACHE_PERMANENT') {
  151. $expire = CACHE_PERMANENT;
  152. }
  153. // Now, we parse the object.
  154. switch (drush_get_option('format', 'string')) {
  155. case 'json':
  156. $data = drush_json_decode($data);
  157. break;
  158. }
  159. if (drush_get_option('cache-get')) {
  160. // $data might be an object.
  161. if (is_object($data) && $data->data) {
  162. $data = $data->data;
  163. }
  164. // But $data returned from `drush cache-get --format=json` will be an array.
  165. elseif (is_array($data) && isset($data['data'])) {
  166. $data = $data['data'];
  167. }
  168. else {
  169. // If $data is neither object nor array and cache-get was specified, then
  170. // there is a problem.
  171. return drush_set_error('CACHE_INVALID_FORMAT', dt("'cache-get' was specified as an option, but the data is neither an object or an array."));
  172. }
  173. }
  174. cache_set($cid, $data, $bin, $expire);
  175. }
  176. /**
  177. * All types of caches available for clearing. Contrib commands can alter in their own.
  178. */
  179. function drush_cache_clear_types($include_bootstraped_types = FALSE) {
  180. $types = array(
  181. 'all' => 'drush_cache_clear_both',
  182. 'drush' => 'drush_cache_clear_drush',
  183. );
  184. if ($include_bootstraped_types) {
  185. $types += array(
  186. 'theme-registry' => 'drush_cache_clear_theme_registry',
  187. 'menu' => 'menu_rebuild',
  188. 'css-js' => 'drush_cache_clear_css_js',
  189. 'block' => 'drush_cache_clear_block',
  190. 'module-list' => 'drush_get_modules',
  191. 'theme-list' => 'drush_get_themes',
  192. );
  193. }
  194. // D8 renamed menu_rebuild() (https://drupal.org/node/1561492).
  195. if (drush_drupal_major_version() >= 8) {
  196. $types['menu'] = 'menu_router_rebuild';
  197. }
  198. if (drush_drupal_major_version() >= 7) {
  199. $types['registry'] = 'registry_update';
  200. }
  201. elseif (drush_drupal_major_version() == 6 && function_exists('module_exists') && module_exists('autoload')) {
  202. // TODO: move this to autoload module.
  203. $types['registry'] = 'autoload_registry_update';
  204. }
  205. // Include the appropriate environment engine, so callbacks can use core
  206. // version specific cache clearing functions directly.
  207. drush_include_engine('drupal', 'environment');
  208. // Command files may customize $types as desired.
  209. drush_command_invoke_all_ref('drush_cache_clear', $types);
  210. return $types;
  211. }
  212. function drush_cache_clear_theme_registry() {
  213. if (drush_drupal_major_version() >= 7) {
  214. drupal_theme_rebuild();
  215. }
  216. else {
  217. cache_clear_all('theme_registry', 'cache', TRUE);
  218. }
  219. }
  220. function drush_cache_clear_css_js() {
  221. _drupal_flush_css_js();
  222. drupal_clear_css_cache();
  223. drupal_clear_js_cache();
  224. }
  225. /**
  226. * Clear the cache of the block output.
  227. */
  228. function drush_cache_clear_block() {
  229. cache_clear_all(NULL, 'cache_block');
  230. }
  231. /**
  232. * Clear caches internal to drush core.
  233. */
  234. function drush_cache_clear_drush() {
  235. drush_cache_clear_all(NULL, 'default'); // commandfiles, etc.
  236. drush_cache_clear_all(NULL, 'complete'); // completion
  237. }
  238. /**
  239. * Clear caches internal to Drush core and Drupal.
  240. */
  241. function drush_cache_clear_both() {
  242. drush_cache_clear_drush();
  243. if (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  244. drupal_flush_all_caches();
  245. }
  246. }