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
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. 'options' => array(
  21. 'format' => 'Format to output the object. Use "print_r" for print_r (default), "export" for var_export, and "json" for JSON.',
  22. ),
  23. 'callback' => 'drush_cache_command_get',
  24. 'aliases' => array('cg'),
  25. );
  26. $items['cache-clear'] = array(
  27. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  28. 'description' => 'Clear a specific cache, or all drupal caches.',
  29. 'arguments' => array(
  30. 'type' => 'The particular cache to clear. Omit this argument to choose from available caches.',
  31. ),
  32. 'callback' => 'drush_cache_command_clear',
  33. 'aliases' => array('cc'),
  34. );
  35. $items['cache-set'] = array(
  36. 'description' => 'Cache an object expressed in JSON or var_export() format.',
  37. 'arguments' => array(
  38. 'cid' => 'The id of the object to set.',
  39. 'data' => 'The object to set in the cache. Use \'-\' to read the object from STDIN.',
  40. 'bin' => 'Optional. The cache bin to store the object in.',
  41. 'expire' => 'Optional. CACHE_PERMANENT, CACHE_TEMPORARY, or a Unix timestamp.',
  42. ),
  43. 'required-arguments' => 2,
  44. 'options' => array(
  45. 'format' => 'Format to parse the object. Use "string" for string (default), and "json" for JSON.',
  46. '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.',
  47. ),
  48. 'callback' => 'drush_cache_command_set',
  49. 'aliases' => array('cs'),
  50. );
  51. return $items;
  52. }
  53. /**
  54. * Command argument complete callback.
  55. *
  56. * @return
  57. * Array of clear types.
  58. */
  59. function cache_cache_command_clear_complete() {
  60. return array('values' => array_keys(drush_cache_clear_types(TRUE)));
  61. }
  62. /**
  63. * Command callback for drush cache-clear.
  64. */
  65. function drush_cache_command_clear($type = NULL) {
  66. $types = drush_cache_clear_types(drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL));
  67. // Check if the provided type ($type) is a valid cache type.
  68. if ($type && !key_exists($type, $types)) {
  69. // If we haven't done a full bootstrap, provide a more
  70. // specific message with instructions to the user on
  71. // bootstrapping a Drupal site for more options.
  72. if (!drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  73. $all_types = drush_cache_clear_types(TRUE);
  74. if (key_exists($type, $all_types)) {
  75. 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)));
  76. }
  77. else {
  78. 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)));
  79. }
  80. }
  81. return drush_set_error(dt("'!type' cache is not a valid cache type.", array('!type' => $type)));
  82. }
  83. if ($type) {
  84. drush_op($types[$type]);
  85. if ($type == 'all' && !drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  86. $type = 'drush';
  87. }
  88. }
  89. else {
  90. // Don't offer 'all' unless Drush has bootstrapped the Drupal site
  91. if (!drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  92. unset($types['all']);
  93. }
  94. $type = drush_choice($types, 'Enter a number to choose which cache to clear.', '!key');
  95. if ($type !== FALSE) {
  96. call_user_func($types[$type]);
  97. }
  98. }
  99. if ($type !== FALSE) {
  100. $site_label = '';
  101. if ($type != 'drush') {
  102. $self_name = drush_sitealias_bootstrapped_site_name();
  103. if (isset($self_name)) {
  104. $site_label = dt(' in !name', array('!name' => $self_name));
  105. }
  106. }
  107. drush_log(dt("'!name' cache was cleared!insitename", array('!name' => $type, '!insitename' => $site_label)), 'success');
  108. }
  109. }
  110. /**
  111. * Print an object returned from the cache.
  112. *
  113. * @param $cid
  114. * The cache ID of the object to fetch.
  115. * @param $bin
  116. * Optional parameter to specify a specific bin to fetch from.
  117. */
  118. function drush_cache_command_get($cid = NULL, $bin = NULL) {
  119. if (!$cid) {
  120. drush_log(dt('You must specify a cache id to fetch.'), 'error');
  121. return;
  122. }
  123. if (!$bin) {
  124. $bin = 'cache';
  125. }
  126. $result = cache_get($cid, $bin);
  127. if (!empty($result)) {
  128. drush_print(drush_format($result));
  129. }
  130. else {
  131. drush_log(dt('The !cid object in the !bin cache bin was not found.', array('!cid' => $cid, '!bin' => $bin)), 'error');
  132. }
  133. }
  134. /**
  135. * Set an object in the cache.
  136. *
  137. * @param $cid
  138. * The cache ID of the object to fetch.
  139. * @param $data
  140. * The data to save to the cache, or '-' to read from STDIN.
  141. * @param $bin
  142. * Optional parameter to specify a specific bin to fetch from.
  143. * @param $expire
  144. * Optional parameter to specify the expiry of the cached object.
  145. */
  146. function drush_cache_command_set($cid = NULL, $data = '', $bin = NULL, $expire = CACHE_PERMANENT) {
  147. if (!$bin) {
  148. $bin = 'cache';
  149. }
  150. if ($data == '-') {
  151. $data = stream_get_contents(STDIN);
  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 = $data->data;
  161. }
  162. cache_set($cid, $data, $bin, $expire);
  163. }
  164. /*
  165. * All types of caches available for clearing. Contrib commands can alter in their own.
  166. */
  167. function drush_cache_clear_types($include_bootstraped_types = FALSE) {
  168. $types = array(
  169. 'all' => 'drush_cache_clear_both',
  170. 'drush' => 'drush_cache_clear_drush',
  171. );
  172. if ($include_bootstraped_types) {
  173. $types += array(
  174. 'theme-registry' => 'drush_cache_clear_theme_registry',
  175. 'menu' => 'menu_rebuild',
  176. 'css-js' => 'drush_cache_clear_css_js',
  177. 'block' => 'drush_cache_clear_block',
  178. 'module-list' => 'drush_get_modules',
  179. 'theme-list' => 'drush_get_themes',
  180. );
  181. }
  182. if (drush_drupal_major_version() >= 7) {
  183. $types['registry'] = 'registry_update';
  184. }
  185. elseif (drush_drupal_major_version() == 6 && function_exists('module_exists') && module_exists('autoload')) {
  186. // TODO: move this to autoload module.
  187. $types['registry'] = 'autoload_registry_update';
  188. }
  189. // Include the appropriate environment engine, so callbacks can use core
  190. // version specific cache clearing functions directly.
  191. drush_include_engine('drupal', 'environment');
  192. // Command files may customize $types as desired.
  193. drush_command_invoke_all_ref('drush_cache_clear', $types);
  194. return $types;
  195. }
  196. function drush_cache_clear_theme_registry() {
  197. if (drush_drupal_major_version() >= 7) {
  198. drupal_theme_rebuild();
  199. }
  200. else {
  201. cache_clear_all('theme_registry', 'cache', TRUE);
  202. }
  203. }
  204. function drush_cache_clear_css_js() {
  205. _drupal_flush_css_js();
  206. drupal_clear_css_cache();
  207. drupal_clear_js_cache();
  208. }
  209. /**
  210. * Clear the cache of the block output.
  211. */
  212. function drush_cache_clear_block() {
  213. cache_clear_all(NULL, 'cache_block');
  214. }
  215. /**
  216. * Clear caches internal to drush core.
  217. */
  218. function drush_cache_clear_drush() {
  219. drush_cache_clear_all(NULL, 'default'); // commandfiles, etc.
  220. drush_cache_clear_all(NULL, 'complete'); // completion
  221. }
  222. /**
  223. * Clear caches internal to Drush core and Drupal.
  224. */
  225. function drush_cache_clear_both() {
  226. drush_cache_clear_drush();
  227. if (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  228. drupal_flush_all_caches();
  229. }
  230. }