memcache.drush.inc 3.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<?php
/**
 * @file
 * Provides a drush interface to Memcached.
 */

/**
 * Implements hook_drush_command().
 */
function memcache_drush_command() {
  $items['memcache-stats'] = array(
    'description' => dt('Retrieve statistics from memcache.'),
    'arguments' => array(
      'bin' => dt('Optionally specify which bin; defaults to \'cache\'.'),
      'type' => dt('Optionally specify type of statistics; one of {default, reset, malloc, slabs, cachedump, items, sizes}.'),
    ),
    'options' => array(
      'interactive' => dt('Interactivly specify which type of statistics to display.'),
      'aggregate' => dt('Included combined statistics from all servers.'),
    ),
    'required-arguments' => 0,
    'examples' => array(
      'memcache-stats' => 'Display raw statistics.',
      'memcache-stats --aggregate' => 'Display raw statistics for all servers and combined totals',
      'memcache-stats --interactive' => 'Interactively select which type of statistics to display.',
      'memcache-stats cache slabs' => 'Display slabs allocated for cache bin.',
    ),
    'aliases' => array('mcs'),
  );

31
  $items['memcache-flush'] = array(
32 33 34 35 36 37 38 39 40
    'description' => dt('Flush all objects from a bin.'),
    'arguments' => array(
      'bin' => dt('Optionally specify which bin to flush; defaults to \'cache\'.'),
    ),
    'required-arguments' => 0,
    'examples' => array(
      'memcache-clear' => 'Flush all items in \'cache\' bin.',
      'memcache-clear cache-page' => 'Flush all items in \'cache-page\' bin.',
    ),
41
    'aliases' => array('mcf'),
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  );

  return $items;
}

/**
 * Display memcache statistics.
 *
 * @param string $bin
 *   The bin to retrieve statistics for.
 * @param string $stats_type
 *   The type of statistics to retrieve when using the Memcache extension.
 */
function drush_memcache_stats($bin = 'cache', $stats_type = 'default') {
  if (_memcache_is_available()) {
    $interactive = drush_get_option('interactive', 0);
    $aggregate = drush_get_option('aggregate', 0);
    if ($interactive) {
      $options = array(
        'default' => 'default',
        'reset' => 'reset',
        'malloc' => 'malloc',
        'slabs' => 'slabs',
        'cachedump' => 'cachedump',
        'items' => 'items',
        'sizes' => 'sizes',
      );
      $stats_type = drush_choice($options, dt('What type of statistics would you like to see?'));
      // stats_type of FALSE === Cancel
      if ($stats_type === FALSE) {
        return;
      }
    }
    $stats = dmemcache_stats($bin, $stats_type, $aggregate);

    drush_print_r($stats);
  }
}

/**
 * Invalidate all items in specified bin.
 *
 * @param string $bin
 *   The bin to flush. Note that this will flush all bins mapped to the same
 *   server as $bin. There is no way at this time to empty just one bin.
 *
 * @return bool
 *   Returns TRUE on success or FALSE on failure.
 */
91
function drush_memcache_flush($bin = 'cache') {
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  if (_memcache_is_available()) {
    $flushed = dmemcache_flush($bin);

    if ($flushed === TRUE) {
      drush_log(dt('Successfully cleared !bin bin.', array('!bin' => $bin)), 'ok');
    }
    else {
      drush_log(dt('Failed to clear !bin bin.', array('!bin' => $bin)), 'error');
    }
  }
}

/**
 * Implements drush_hook_COMMAND_validate().
 */
107
function drush_memcache_flush_validate() {
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  return _memcache_is_available();
}

/**
 * Implements drush_hook_COMMAND_validate().
 */
function drush_memcached_stats_validate() {
  return _memcache_is_available();
}

/**
 * Check if memcache.inc has been included.
 */
function _memcache_is_available() {
  if (!class_exists('MemCacheDrupal', FALSE)) {
    return drush_set_error("MemCacheDrupal class is not available", "Please review README.txt and properly enable memcache.");;
  }
  return TRUE;
}