Enable Object Cache

Learn how to enable Object Cache on your site.

Contributors: Christopher Matthews, Carolyn Shannon, Jordan Schulz, Whitney Meredith.

Discuss in our Forum Discuss in Slack

This section provides information on how to enable Object Cache.

  1. Navigate to your Pantheon Site Dashboard, select Settings, select Add Ons, then select Add. It might take a couple of minutes for the Object Cache server to come online.

  2. Install the WP Redis plugin via SFTP or Git. You can also install the plugin via Terminus if you set the connection mode to SFTP and run:

    terminus wp <site>.<env> -- plugin install wp-redis

    If you use site networks, you must add the site URL by adding to the command:

    terminus wp <site>.<env> -- plugin install wp-redis --url=<url>
  3. Create a new file named wp-content/object-cache.php that contains the following:

    "object-cache.php"
    <?php
    # This is a Windows-friendly symlink
    if (!empty($_ENV['PANTHEON_ENVIRONMENT']) && !empty($_ENV['CACHE_HOST'])) {
      require_once WP_CONTENT_DIR . '/plugins/wp-redis/object-cache.php';
    }

    This file is a symlink to the /plugins/wp-redis/object-cache.php file. Use SFTP or Git to commit the new file to the Dev environment.

  4. Navigate to the Dev environment's WordPress Dashboard, and verify the installation by selecting Drop-ins from the Plugins section:

    The object-cache Drop-In Plugin

    You can upgrade by the normal Plugin update mechanism in WordPress or via Terminus when a new version of the WP Redis plugin is released:

    terminus wp <site>.<env> -- plugin update wp-redis

 Note

WP Redis is loaded via a drop-in file, so there's no need to activate it on your WordPress sites.

 Note

This configuration uses the Redis_CacheCompressed class for better performance. This requires the Redis module version 3.13 or later. For versions before 3.13, use Redis_Cache in step 4 instead.

  1. Navigate to your Pantheon Site Dashboard, select Settings, select Add Ons, then select Add to enable the Redis cache server. It might take a couple of minutes for the Redis server to come online.

  2. Add the Redis module from Drupal.org. You can install and enable the module from the command line using Terminus:

    terminus remote:drush <site>.<env> -- en redis -y
  3. Ignore the directions bundled with the Redis module. Pantheon automatically manages the following settings.php/$conf/variable_get items for you:

    • redis_client_host
    • redis_client_port
    • redis_client_password
  4. Edit sites/default/settings.php to add the Redis cache configuration. These are the mandatory, required Redis configurations for every site.

    settings.php
    // All Pantheon Environments.
    if (!empty($_ENV['PANTHEON_ENVIRONMENT']) && !empty($_ENV['CACHE_HOST'])) {
      // Use Redis for caching.
      $conf['redis_client_interface'] = 'PhpRedis';
      // Point Drupal to the location of the Redis plugin.
      $conf['cache_backends'][] = 'sites/all/modules/redis/redis.autoload.inc';
      // If you've installed your plugin in a contrib directory, use this line instead:
      // $conf['cache_backends'][] = 'sites/all/modules/contrib/redis/redis.autoload.inc';
      $conf['cache_default_class'] = 'Redis_CacheCompressed';
      $conf['cache_prefix'] = array('default' => 'pantheon-redis');
      // Do not use Redis for cache_form (no performance difference).
      $conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
      // Use Redis for Drupal locks (semaphore).
      $conf['lock_inc'] = 'sites/all/modules/redis/redis.lock.inc';
      // Or if you've installed the redis module in a contrib subdirectory, use:
      // $conf['lock_inc'] = 'sites/all/modules/contrib/redis/redis.lock.inc';
    }
  5. Enable the module via /admin/modules, if you haven't already done so, using Terminus.

  6. Navigate to the dashboard and click Connection Info to verify that Redis is enabled. If you see the Redis cache connection string, Redis is enabled.

  7. Visit /admin/config/development/performance/redis and open Connection Information to verify the connection.

  1. Clone the code repository and run the following from the project root:

    terminus connection:set $SITE.dev git
    terminus redis:enable $SITE
    composer require drupal/redis
    git add . && git commit -m "Add drupal/redis dependency" && git push origin master
  2. Enable the new Redis module and export configuration:

    terminus connection:set $SITE.dev sftp
    terminus drush $SITE.dev -- en redis -y
    terminus drush $SITE.dev -- config:export -y
    terminus env:commit $SITE.dev --message="Enable Redis, export configuration"
  3. Edit sites/default/settings.php to add the Redis cache configuration. These are mandatory, required Redis configurations for every site:

    sites/default/settings.php
    // Configure Redis
    
    if (!empty($_ENV['PANTHEON_ENVIRONMENT']) && !empty($_ENV['CACHE_HOST'])) {
      // Include the Redis services.yml file. Adjust the path if you installed to a contrib or other subdirectory.
      $settings['container_yamls'][] = 'modules/redis/example.services.yml';
    
      //phpredis is built into the Pantheon application container.
      $settings['redis.connection']['interface'] = 'PhpRedis';
      // These are dynamic variables handled by Pantheon.
      $settings['redis.connection']['host']      = $_ENV['CACHE_HOST'];
      $settings['redis.connection']['port']      = $_ENV['CACHE_PORT'];
      $settings['redis.connection']['password']  = $_ENV['CACHE_PASSWORD'];
    
      $settings['redis_compress_length'] = 100;
      $settings['redis_compress_level'] = 1;
    
      $settings['cache']['default'] = 'cache.backend.redis'; // Use Redis as the default cache.
      $settings['cache_prefix']['default'] = 'pantheon-redis';
    
      $settings['cache']['bins']['form'] = 'cache.backend.database'; // Use the database for forms
    }

     Note

    The above Redis cache configuration should be placed in sites/default/settings.php, rather than settings.pantheon.php, to avoid conflicts with future upstream updates.

More Resources