Skip to content

Instantly share code, notes, and snippets.

@briantully
Created July 26, 2018 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briantully/836d7798b57ea3f1a1422776a31685a1 to your computer and use it in GitHub Desktop.
Save briantully/836d7798b57ea3f1a1422776a31685a1 to your computer and use it in GitHub Desktop.
Acquia D8 Memcache Sample Configuration
/**
* @file
* Example implementation of ACSF post-settings-php hook.
*
* @see https://docs.acquia.com/site-factory/tiers/paas/workflow/hooks
*/
// The path to this file should be docroot/factory-hooks/post-settings-php/memcache.php
// Use ACSF internal settings site flag to apply memcache settings.
if (getenv('AH_SITE_ENVIRONMENT') &&
isset($site_settings['flags']['memcache_enabled']) &&
isset($settings['memcache']['servers'])
) {
require '[path-to-project-settings]/memcacheD8.settings.php';
}
services:
# Replaces the default lock backend with a memcache implementation.
lock:
class: Drupal\Core\Lock\LockBackendInterface
factory: memcache.lock.factory:get
# Replaces the default persistent lock backend with a memcache implementation.
lock.persistent:
class: Drupal\Core\Lock\LockBackendInterface
factory: memcache.lock.factory:getPersistent
<?php
/**
* @file
* Contains memcache configuration.
*/
use Composer\Autoload\ClassLoader;
/**
* Use memcache as cache backend.
*
* Autoload memcache classes and service container in case module is not
* installed. Avoids the need to patch core and allows for overriding the
* default backend when installing Drupal.
*
* @see https://www.drupal.org/node/2766509
*/
if (getenv('AH_SITE_ENVIRONMENT') &&
isset($settings['memcache']['servers'])
) {
// Check for PHP Memcached libraries.
$memcache_exists = class_exists('Memcache', FALSE);
$memcached_exists = class_exists('Memcached', FALSE);
$memcache_module_is_present = file_exists(DRUPAL_ROOT . '/modules/contrib/memcache/memcache.services.yml');
if ($memcache_module_is_present && ($memcache_exists || $memcached_exists)) {
// Use Memcached extension if available.
if ($memcached_exists) {
$settings['memcache']['extension'] = 'Memcached';
}
if (class_exists(ClassLoader::class)) {
$class_loader = new ClassLoader();
$class_loader->addPsr4('Drupal\\memcache\\', 'modules/contrib/memcache/src');
$class_loader->register();
$settings['container_yamls'][] = DRUPAL_ROOT . '/modules/contrib/memcache/memcache.services.yml';
// Bootstrap cache.container with memcache rather than database.
$settings['bootstrap_container_definition'] = [
'parameters' => [],
'services' => [
'database' => [
'class' => 'Drupal\Core\Database\Connection',
'factory' => 'Drupal\Core\Database\Database::getConnection',
'arguments' => ['default'],
],
'settings' => [
'class' => 'Drupal\Core\Site\Settings',
'factory' => 'Drupal\Core\Site\Settings::getInstance',
],
'memcache.config' => [
'class' => 'Drupal\memcache\DrupalMemcacheConfig',
'arguments' => ['@settings'],
],
'memcache.backend.cache.factory' => [
'class' => 'Drupal\memcache\DrupalMemcacheFactory',
'arguments' => ['@memcache.config'],
],
'memcache.backend.cache.container' => [
'class' => 'Drupal\memcache\DrupalMemcacheFactory',
'factory' => ['@memcache.backend.cache.factory', 'get'],
'arguments' => ['container'],
],
'lock.container' => [
'class' => 'Drupal\memcache\Lock\MemcacheLockBackend',
'arguments' => ['container', '@memcache.backend.cache.container'],
],
'cache_tags_provider.container' => [
'class' => 'Drupal\Core\Cache\DatabaseCacheTagsChecksum',
'arguments' => ['@database'],
],
'cache.container' => [
'class' => 'Drupal\memcache\MemcacheBackend',
'arguments' => [
'container',
'@memcache.backend.cache.container',
'@lock.container',
'@memcache.config',
'@cache_tags_provider.container',
],
],
],
];
// Override default fastchained backend for static bins.
// @see https://www.drupal.org/node/2754947
$settings['cache']['bins']['bootstrap'] = 'cache.backend.memcache';
$settings['cache']['bins']['discovery'] = 'cache.backend.memcache';
$settings['cache']['bins']['config'] = 'cache.backend.memcache';
// Use memcache as the default bin.
$settings['cache']['default'] = 'cache.backend.memcache';
// Enable stampede protection.
$settings['memcache']['stampede_protection'] = TRUE;
// Move locks to memcache (This path may need to be modified)
$settings['container_yamls'][] = 'memcache.yml';
// Enable compression for PHP 7.
$settings['memcache']['options'] = [
Memcached::OPT_COMPRESSION => TRUE,
];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment