memcache.drush.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @file
  4. * Provides a drush interface to Memcached.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. */
  9. function memcache_drush_command() {
  10. $items['memcache-stats'] = array(
  11. 'description' => dt('Retrieve statistics from memcache.'),
  12. 'arguments' => array(
  13. 'bin' => dt('Optionally specify which bin; defaults to \'cache\'.'),
  14. 'type' => dt('Optionally specify type of statistics; one of {default, reset, malloc, slabs, cachedump, items, sizes}.'),
  15. ),
  16. 'options' => array(
  17. 'interactive' => dt('Interactivly specify which type of statistics to display.'),
  18. 'aggregate' => dt('Included combined statistics from all servers.'),
  19. ),
  20. 'required-arguments' => 0,
  21. 'examples' => array(
  22. 'memcache-stats' => 'Display raw statistics.',
  23. 'memcache-stats --aggregate' => 'Display raw statistics for all servers and combined totals',
  24. 'memcache-stats --interactive' => 'Interactively select which type of statistics to display.',
  25. 'memcache-stats cache slabs' => 'Display slabs allocated for cache bin.',
  26. ),
  27. 'aliases' => array('mcs'),
  28. );
  29. $items['memcache-flush'] = array(
  30. 'description' => dt('Flush all objects from a bin.'),
  31. 'arguments' => array(
  32. 'bin' => dt('Optionally specify which bin to flush; defaults to \'cache\'.'),
  33. ),
  34. 'required-arguments' => 0,
  35. 'examples' => array(
  36. 'memcache-clear' => 'Flush all items in \'cache\' bin.',
  37. 'memcache-clear cache-page' => 'Flush all items in \'cache-page\' bin.',
  38. ),
  39. 'aliases' => array('mcf'),
  40. );
  41. return $items;
  42. }
  43. /**
  44. * Display memcache statistics.
  45. *
  46. * @param string $bin
  47. * The bin to retrieve statistics for.
  48. * @param string $stats_type
  49. * The type of statistics to retrieve when using the Memcache extension.
  50. */
  51. function drush_memcache_stats($bin = 'cache', $stats_type = 'default') {
  52. if (_memcache_is_available()) {
  53. $interactive = drush_get_option('interactive', 0);
  54. $aggregate = drush_get_option('aggregate', 0);
  55. if ($interactive) {
  56. $options = array(
  57. 'default' => 'default',
  58. 'reset' => 'reset',
  59. 'malloc' => 'malloc',
  60. 'slabs' => 'slabs',
  61. 'cachedump' => 'cachedump',
  62. 'items' => 'items',
  63. 'sizes' => 'sizes',
  64. );
  65. $stats_type = drush_choice($options, dt('What type of statistics would you like to see?'));
  66. // stats_type of FALSE === Cancel
  67. if ($stats_type === FALSE) {
  68. return;
  69. }
  70. }
  71. $stats = dmemcache_stats($bin, $stats_type, $aggregate);
  72. drush_print_r($stats);
  73. }
  74. }
  75. /**
  76. * Invalidate all items in specified bin.
  77. *
  78. * @param string $bin
  79. * The bin to flush. Note that this will flush all bins mapped to the same
  80. * server as $bin. There is no way at this time to empty just one bin.
  81. *
  82. * @return bool
  83. * Returns TRUE on success or FALSE on failure.
  84. */
  85. function drush_memcache_flush($bin = 'cache') {
  86. if (_memcache_is_available()) {
  87. $flushed = dmemcache_flush($bin);
  88. if ($flushed === TRUE) {
  89. drush_log(dt('Successfully cleared !bin bin.', array('!bin' => $bin)), 'ok');
  90. }
  91. else {
  92. drush_log(dt('Failed to clear !bin bin.', array('!bin' => $bin)), 'error');
  93. }
  94. }
  95. }
  96. /**
  97. * Implements drush_hook_COMMAND_validate().
  98. */
  99. function drush_memcache_flush_validate() {
  100. return _memcache_is_available();
  101. }
  102. /**
  103. * Implements drush_hook_COMMAND_validate().
  104. */
  105. function drush_memcached_stats_validate() {
  106. return _memcache_is_available();
  107. }
  108. /**
  109. * Check if memcache.inc has been included.
  110. */
  111. function _memcache_is_available() {
  112. if (!class_exists('MemCacheDrupal', FALSE)) {
  113. return drush_set_error("MemCacheDrupal class is not available", "Please review README.txt and properly enable memcache.");;
  114. }
  115. return TRUE;
  116. }