Enable Object Cache

Learn how to enable Object Cache on your site.

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 WordPress Multisite, 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
    $objectCacheFile = WP_CONTENT_DIR . '/plugins/wp-redis/object-cache.php';
    if (!empty($_ENV['PANTHEON_ENVIRONMENT']) && !empty($_ENV['CACHE_HOST']) && file_exists($objectCacheFile)) {
        require_once $objectCacheFile;
    }

    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. 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. 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
  3. 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"
  4. 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 (defined(
     'PANTHEON_ENVIRONMENT'
    ) && !\Drupal\Core\Installer\InstallerKernel::installationAttempted(
    ) && extension_loaded('redis')) {
     // Set Redis as the default backend for any cache bin not otherwise specified.
     $settings['cache']['default'] = 'cache.backend.redis';
    
     //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_prefix']['default'] = 'pantheon-redis';
    
     $settings['cache']['bins']['form'] = 'cache.backend.database'; // Use the database for forms
    
     // Apply changes to the container configuration to make better use of Redis.
     // This includes using Redis for the lock and flood control systems, as well
     // as the cache tag checksum. Alternatively, copy the contents of that file
     // to your project-specific services.yml file, modify as appropriate, and
     // remove this line.
     $settings['container_yamls'][] = 'modules/contrib/redis/example.services.yml';
    
     // Allow the services to work before the Redis module itself is enabled.
     $settings['container_yamls'][] = 'modules/contrib/redis/redis.services.yml';
    
     // Manually add the classloader path, this is required for the container
     // cache bin definition below.
     $class_loader->addPsr4('Drupal\\redis\\', 'modules/contrib/redis/src'); 
    
     // Use redis for container cache.
     // The container cache is used to load the container definition itself, and
     // thus any configuration stored in the container itself is not available
     // yet. These lines force the container cache to use Redis rather than the
     // default SQL cache.
     $settings['bootstrap_container_definition'] = [
       'parameters' => [],
       'services' => [
         'redis.factory' => [
           'class' => 'Drupal\redis\ClientFactory',
         ],
         'cache.backend.redis' => [
           'class' => 'Drupal\redis\Cache\CacheBackendFactory',
           'arguments' => [
             '@redis.factory',
             '@cache_tags_provider.container',
             '@serialization.phpserialize',
           ],
         ],
         'cache.container' => [
           'class' => '\Drupal\redis\Cache\PhpRedis',
           'factory' => ['@cache.backend.redis', 'get'],
           'arguments' => ['container'],
         ],
         'cache_tags_provider.container' => [
           'class' => 'Drupal\redis\Cache\RedisCacheTagsChecksum',
           'arguments' => ['@redis.factory'],
         ],
         'serialization.phpserialize' => [
           'class' => 'Drupal\Component\Serialization\PhpSerialize',
         ],
       ],
     ];
    }
    

     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