Drush commands to enable/disable performance options in Drupal 7

Drush commands to enable/disable performance and cache related options in Drupal 7

The script presented in this post adds two new drush commands: cache-enable and cache-disable which gives us a handy way to disable / enable performance and cache related configuration parameters and options. These parameters can be found in the admin/config/development/performance section of a Drupal 7 installation.

The cache-enable actually performs all the following actions:
  1. Enables the "Cache pages for anonymous users" option
  2. Enables the "Cache blocks" option
  3. Sets the "Minimum cache lifetime" option to the required value. Defaults to 1 minute
  4. Sets the "Expiration of cached pages" option to the required value. Defaults to 1 hour 
  5. Enables the "Aggregate and compress CSS files" option
  6. Enables the "Aggregate JavaScript files" option 
  7. Finally clears all caches (same as pressing the "Clear All Caches" button)

The command cache-disable reverses all of the above actions to either false or 0 values:
  1. Disables the "Cache pages for anonymous users" option
  2. Disables the "Cache blocks" option
  3. Sets the "Minimum cache lifetime" option to 0
  4. Sets the "Expiration of cached pages" option to 0
  5. Disables the "Aggregate and compress CSS files" option
  6. Disables the "Aggregate JavaScript files" option 
  7. Finally clears all caches (same as pressing the "Clear All Caches" button)
This is a screenshot from a Drupal 7 admin/config/development/performance section. The new commands affect all of the options shown:
Drupal 7 basic performance options
The commands can be used for example when you are in a development environment and you want to quickly disable / enable performance related parameters. It is especially useful in multisite configurations.

Usage

The run this command just go to your drupal homedir and run it like any other drush command:
drush cache-disable (ccd)
drush cache-enable (cce)
The cache-enable command can be invoked using your own required values for the "Minimum cache lifetime" and "Expiration of cached pages" options using the following command syntax:
drush cache-enable <Minimum cache lifetime> <Expiration of cached pages>
For example:
drush cache-enable 120 7200
The above command invokes cache-enable with 'Minimum cache lifetime' set to 120 seconds and 'Expiration of cached pages' set to 7200 seconds.

You can even get help running the commands using:
drush help cce
Test run:
root@host:/var/vhosts/drupal# drush cache-enable
Cache enabled.                                                                    [ok]
Blocks cache enabled.                                                             [ok]
Minimum cache lifetime set to 60 seconds.                                         [ok]
Expiration of cached pages set to 3600 seconds.                                   [ok]
Aggregate and compress CSS files option enabled                                   [ok]
Aggregate JavaScript files option enabled                                         [ok]
All caches cleared.                                                               [ok]

Multisite

The command is also compatible with multisite installs. It can be invoked using the usual "multisite" method:
drush @sites cache-disable
drush @sites cache-enable
or for just a specific site:
drush -l site1.example.com cache-enable

Commands script

In order to use the new commands just copy the file below, place it under your $HOME/.drush directory and name it mycache.drush.inc
You have to use the exact name mentioned in order to execute this command otherwise drush won't be aware of the new command. You can also use other locations for the script as shown in the comments section.

<?php

/**
 * @file
 *
 * Enables / Disables all cache related parameters found in /admin/config/development/performance
 * Author: Mihalis Miliarakis
 * Date: 20150526
 *
 * You can copy this file to any of the following
 *   1. A .drush folder in your HOME folder.
 *   2. Anywhere in a folder tree below an active module on your site.
 *   3. /usr/share/drush/commands (configurable)
 *   4. In an arbitrary folder specified with the --include option.
 *   5. Drupal's /drush or /sites/all/drush folders, or in the /drush
 *        folder in the directory above the Drupal root.
 */

/**
 * Implements mycache_drush_command().
 */
function mycache_drush_command() {
  $items = array();

  $items['cache-disable'] = array(
    'description' => "Disables all cache related parameters found in /admin/config/development/performance.",
    'aliases' => array('ccd'),
  );
  $items['cache-enable'] = array(
    'description' => "Enables all cache related parameters found in /admin/config/development/performance.",
    'arguments' => array(
        'min' => "Minimum cache lifetime in seconds",
        'max' => "Expiration of cached pages in seconds"
    ),
    'aliases' => array('cce'),
    'examples' => array(
        'drush cache-enable' => "cache-enable without parameters uses default values for 'Minimum cache lifetime' set to 60 seconds and 'Expiration of cached pages' set to 3600 seconds.",
        'drush cache-enable 120 7200' => "cache-enable with 'Minimum cache lifetime' set to 120 seconds and 'Expiration of cached pages' set to 7200 seconds.",
    ),
  );

  return $items;
}

/**
 * Implements cache-disable command
 */
function drush_mycache_cache_disable() {
  variable_set('cache', 0);
  drush_log('Cache disabled.', 'ok');
  variable_set('block_cache', 0);
  drush_log('Blocks cache disabled.', 'ok');
  variable_set('cache_lifetime', 0);
  drush_log('Minimum cache lifetime set to 0', 'ok');
  variable_set('page_cache_maximum_age', 0);
  drush_log('Expiration of cached pages set to 0','ok');
  variable_set('preprocess_css', 0);
  drush_log('Aggregate and compress CSS files option disabled', 'ok');
  variable_set('preprocess_js', 0);
  drush_log('Aggregate JavaScript files option disabled', 'ok');
  drupal_flush_all_caches();
  drush_log('All caches cleared.', 'ok');
}

/**
 * Implements cache-enable command
 */
function drush_mycache_cache_enable($min = 60, $max = 3600) {
  variable_set('cache', TRUE);
  drush_log('Cache enabled.', 'ok');
  variable_set('block_cache', TRUE);
  drush_log('Blocks cache enabled.', 'ok');
  variable_set('cache_lifetime', $min);
  drush_log("Minimum cache lifetime set to $min seconds.", 'ok');
  variable_set('page_cache_maximum_age', $max);
  drush_log("Expiration of cached pages set to $max seconds.", 'ok');
  variable_set('preprocess_css', TRUE);
  drush_log('Aggregate and compress CSS files option enabled', 'ok');
  variable_set('preprocess_js', TRUE);
  drush_log('Aggregate JavaScript files option enabled', 'ok');
  drupal_flush_all_caches();
  drush_log('All caches cleared.', 'ok');
}




No comments:

Post a Comment