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_drush_command Implementation of hook_drush_command().
drush_cache_clear Command callback for drush cache-clear.
drush_cache_clear_block Clear the cache of the block output.
drush_cache_clear_css_js
drush_cache_clear_theme_registry
drush_cache_clear_types
drush_cache_get Print an object returned from the cache.
drush_cache_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. $items['cache-get'] = array(
  8. 'description' => 'Fetch a cached object and display it.',
  9. 'examples' => array(
  10. 'drush cache-get schema' => 'Display the data for the cache id "schema" from the "cache" bin.',
  11. 'drush cache-get update_available_releases update' => 'Display the data for the cache id "update_available_releases" from the "update" bin.',
  12. ),
  13. 'arguments' => array(
  14. 'cid' => 'The id of the object to fetch.',
  15. 'bin' => 'Optional. The cache bin to fetch from.',
  16. ),
  17. 'options' => array(
  18. 'format' => 'Format to output the object. Use "print_r" for print_r (default), "export" for var_export, and "json" for JSON.',
  19. ),
  20. 'aliases' => array('cg'),
  21. );
  22. $items['cache-clear'] = array(
  23. 'description' => 'Clear a specific cache, or all drupal caches.',
  24. 'arguments' => array(
  25. 'type' => 'The particular cache to clear. Omit this argument to choose from available caches.',
  26. ),
  27. 'aliases' => array('cc'),
  28. );
  29. $items['cache-set'] = array(
  30. 'description' => 'Cache an object expressed in JSON or var_export() format.',
  31. 'arguments' => array(
  32. 'cid' => 'The id of the object to set.',
  33. 'data' => 'The object to set in the cache. Use \'-\' to read the object from STDIN.',
  34. 'bin' => 'Optional. The cache bin to store the object in.',
  35. 'expire' => 'Optional. CACHE_PERMANENT, CACHE_TEMPORARY, or a Unix timestamp.',
  36. ),
  37. 'options' => array(
  38. 'format' => 'Format to parse the object. Use "string" for string (default), and "json" for JSON.',
  39. '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.',
  40. ),
  41. 'aliases' => array('cs'),
  42. );
  43. return $items;
  44. }
  45. /**
  46. * Command callback for drush cache-clear.
  47. */
  48. function drush_cache_clear($type = NULL) {
  49. switch (drush_drupal_major_version()) {
  50. case 5:
  51. // clear preprocessor cache
  52. drupal_clear_css_cache();
  53. // clear core tables
  54. $core = array('cache', 'cache_filter', 'cache_menu', 'cache_page');
  55. $alltables = array_merge($core, module_invoke_all('devel_caches'));
  56. foreach ($alltables as $table) {
  57. cache_clear_all('*', $table, TRUE);
  58. }
  59. drush_print(dt('Cache cleared.'));
  60. break;
  61. case 6:
  62. case 7:
  63. default:
  64. $types = drush_cache_clear_types();
  65. // Check if the provided type ($type) is a valid cache type.
  66. if ($type && !key_exists($type, $types)) {
  67. return drush_set_error(dt("'!type' cache is not a valid cache type", array('!type' => $type)));
  68. }
  69. if ($type) {
  70. drush_op($types[$type]);
  71. drush_log(dt("'!name' cache was cleared", array('!name' => $type)), 'success');
  72. }
  73. else {
  74. $choice = drush_choice($types, 'Enter a number to choose which cache to clear.', '!key');
  75. if ($choice !== FALSE) {
  76. call_user_func($types[$choice]);
  77. drush_log(dt("'!name' cache was cleared", array('!name' => $choice)), 'success');
  78. }
  79. }
  80. break;
  81. }
  82. }
  83. /**
  84. * Print an object returned from the cache.
  85. *
  86. * @param $cid
  87. * The cache ID of the object to fetch.
  88. * @param $bin
  89. * Optional parameter to specify a specific bin to fetch from.
  90. */
  91. function drush_cache_get($cid = NULL, $bin = NULL) {
  92. if (!$cid) {
  93. drush_log(dt('You must specify a cache id to fetch.'), 'error');
  94. return;
  95. }
  96. if (!$bin) {
  97. $bin = 'cache';
  98. }
  99. $result = cache_get($cid, $bin);
  100. if (!empty($result)) {
  101. switch (drush_get_option('format', 'print_r')) {
  102. case 'export':
  103. drush_print(var_export($result, TRUE));
  104. return;
  105. case 'json':
  106. drush_print(drush_json_encode($result));
  107. return;
  108. case 'print_r':
  109. default:
  110. drush_print_r($result);
  111. return;
  112. }
  113. }
  114. else {
  115. drush_log(dt('The !cid object in the !bin cache bin was not found.', array('!cid' => $cid, '!bin' => $bin)), 'error');
  116. }
  117. }
  118. /**
  119. * Set an object in the cache.
  120. *
  121. * @param $cid
  122. * The cache ID of the object to fetch.
  123. * @param $data
  124. * The data to save to the cache, or '-' to read from STDIN.
  125. * @param $bin
  126. * Optional parameter to specify a specific bin to fetch from.
  127. * @param $expire
  128. * Optional parameter to specify the expiry of the cached object.
  129. */
  130. function drush_cache_set($cid = NULL, $data = '', $bin = NULL, $expire = CACHE_PERMANENT) {
  131. if (!$cid) {
  132. drush_log(dt('You must specify a cache id to set.'), 'error');
  133. return;
  134. }
  135. if ($data === '') {
  136. drush_log(dt('The data to set must be non-empty.'), 'error');
  137. return;
  138. }
  139. if (!$bin) {
  140. $bin = 'cache';
  141. }
  142. if ($data == '-') {
  143. $data = stream_get_contents(STDIN);
  144. }
  145. // Now, we parse the object.
  146. switch (drush_get_option('format', 'string')) {
  147. case 'json':
  148. $data = drush_json_decode($data);
  149. break;
  150. }
  151. if (drush_get_option('cache-get')) {
  152. $data = $data->data;
  153. }
  154. switch(drush_drupal_major_version()) {
  155. case 5:
  156. cache_set($cid, $bin, $data, $expire);
  157. break;
  158. case 6:
  159. case 7:
  160. default:
  161. cache_set($cid, $data, $bin, $expire);
  162. }
  163. }
  164. function drush_cache_clear_types() {
  165. $types = array(
  166. 'all' => 'drupal_flush_all_caches',
  167. 'theme registry' => 'drush_cache_clear_theme_registry',
  168. 'menu' => 'menu_rebuild',
  169. 'css+js' => 'drush_cache_clear_css_js',
  170. 'block' => 'drush_cache_clear_block',
  171. 'module list' => 'drush_get_modules',
  172. 'theme list' => 'drush_get_themes',
  173. );
  174. if (drush_drupal_major_version() >= 7) {
  175. $types['registry'] = 'registry_update';
  176. }
  177. elseif (drush_drupal_major_version() == 6 && module_exists('autoload')) {
  178. // TODO: move this to autoload module.
  179. $types['registry'] = 'autoload_registry_update';
  180. }
  181. if (count(module_implements('node_grants'))) {
  182. $types['nodeaccess'] = 'node_access_rebuild';
  183. }
  184. // Include the appropriate environment engine, so callbacks can use core
  185. // version specific cache clearing functions directly.
  186. drush_include_engine('drupal', 'environment');
  187. // Command files may customize $types as desired.
  188. drush_command_invoke_all_ref('drush_cache_clear', $types);
  189. return $types;
  190. }
  191. function drush_cache_clear_theme_registry() {
  192. drush_db_delete('cache', 'cid LIKE :theme_registry', array(':theme_registry' => 'theme_registry%'));
  193. }
  194. function drush_cache_clear_css_js() {
  195. _drupal_flush_css_js();
  196. drupal_clear_css_cache();
  197. drupal_clear_js_cache();
  198. }
  199. /**
  200. * Clear the cache of the block output.
  201. */
  202. function drush_cache_clear_block() {
  203. cache_clear_all(NULL, 'cache_block');
  204. }