diff -u b/README.txt b/README.txt --- b/README.txt +++ b/README.txt @@ -7,14 +7,14 @@ ## REQUIREMENTS ## -- PHP 5.4 or greater +- PHP 5.5 or greater - Availability of a memcached daemon: http://memcached.org/ - One of the two PECL memcache packages: - http://pecl.php.net/package/memcache (recommended) - http://pecl.php.net/package/memcached For more detailed instructions on installing a memcached daemon or either of the -memcache pecl extensions, please see the documentation online at +memcache PECL extensions, please see the documentation online at https://www.drupal.org/node/1131458 which includes links to external walk-throughs for various operating systems. @@ -23,50 +23,49 @@ These are the steps you need to take in order to use this software. Order is important. - 1. Put your site into offline mode. + 1. Make sure you have one of the PECL memcache packages installed. 2. Enable the memcache module. You need to enable the module in Drupal before you can configure it to run as the default backend. This is so Drupal knows where to find everything. 2. Edit settings.php to configure the servers, clusters and bins that memcache is supposed to use. You do not need to set this if the only memcache backend is localhost on port 11211. By default the main settings will be: - $settings['memcache']['servers'] = array('127.0.0.1:11211' => 'default'); - $settings['memcache']['bins'] = array('default' => 'default'); + $settings['memcache']['servers'] = ['127.0.0.1:11211' => 'default']; + $settings['memcache']['bins'] = ['default' => 'default']; $settings['memcache']['key_prefix'] => ''; 7. Edit settings.php to make memcache the default cache class, for example: $settings['cache']['default'] = 'cache.backend.memcache'; - 9. Bring your site back online. ## ADVANCED CCONFIGURATION ## ### Multiple memcache backends ### - $settings['memcache']['servers'] = array( + $settings['memcache']['servers'] = [ '127.0.0.1:11211' => 'default', // Default host and port '127.0.0.1:11212' => 'default', // Default host with port 11212 '127.0.0.2:11211' => 'default', // Default port, different IP 'server1.com:11211' => 'default', // Default port with hostname 'unix:///path/to/socket' => 'default', 'Unix socket' - ); + ]; ### Multiple servers, bins and clusters ### - $settings['memcache'] = array( - 'servers' = array( + $settings['memcache'] = [ + 'servers' = [ 'server1:port' => 'default', 'server2:port' => 'default', 'server3:port' => 'cluster1', 'serverN:port' => 'clusterN', 'unix:///path/to/socket' => 'clusterS', - ), - 'bins' => array( + ], + 'bins' => [ 'default' => 'default', 'bin1' => 'cluster1', 'binN' => 'clusterN', 'binX' => 'cluster1', 'binS' => 'clusterS', - ), - ); + ], + ]; The bin/cluster/server model can be described as follows: @@ -150,9 +149,9 @@ Memcached options can be controlled in settings.php. Consistent distribution is the default in this case but could be set using: - $setting['memcache']['options'] = array( + $setting['memcache']['options'] = [ Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT, - ); + ]; ## LOCKS ## @@ -195,10 +194,10 @@ default options (selected through performance testing). These options will be set unless overridden in settings.php. - $settings['memcache']['options'] = array( + $settings['memcache']['options'] = [ Memcached::OPT_COMPRESSION => FALSE, Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT, - ); + ]; These are as follows: reverted: --- b/memcache.services.yml +++ a/memcache.services.yml @@ -1,10 +1,7 @@ services: - memcache.config: - class: Drupal\memcache\DrupalMemcacheConfig - arguments: ['@settings'] memcache.factory: class: Drupal\memcache\DrupalMemcacheFactory + arguments: ['@settings'] - arguments: ['@memcache.config'] cache.backend.memcache: class: Drupal\memcache\MemcacheBackendFactory + arguments: ['@lock', '@settings', '@memcache.factory', '@cache_tags.invalidator.checksum'] - arguments: ['@lock', '@memcache.config', '@memcache.factory', '@cache_tags.invalidator.checksum'] reverted: --- b/src/DrupalMemcache.php +++ a/src/DrupalMemcache.php @@ -7,6 +7,7 @@ namespace Drupal\memcache; +use Drupal\Core\Site\Settings; use Psr\Log\LogLevel; /** @@ -17,7 +18,7 @@ /** * {@inheritdoc} */ + public function __construct($bin, Settings $settings) { - public function __construct($bin, DrupalMemcacheConfig $settings) { parent::__construct($bin, $settings); $this->memcache = new \Memcache(); reverted: --- b/src/DrupalMemcacheBase.php +++ a/src/DrupalMemcacheBase.php @@ -7,6 +7,7 @@ namespace Drupal\memcache; +use Drupal\Core\Site\Settings; use Psr\Log\LogLevel; /** @@ -24,7 +25,7 @@ /** * The settings object. * + * @var \Drupal\Core\Site\Settings - * @var \Drupal\memcache\DrupalMemcacheConfig */ protected $settings; @@ -48,14 +49,14 @@ * * @param string $bin * The cache bin. + * @param \Drupal\Core\Site\Settings - * @param \Drupal\memcache\DrupalMemcacheConfig * The settings object. */ + public function __construct($bin, Settings $settings) { - public function __construct($bin, DrupalMemcacheConfig $settings) { $this->bin = $bin; $this->settings = $settings; + $this->hashAlgorithm = $this->settings->get('memcache_key_hash_algorithm', 'sha1'); - $this->hashAlgorithm = $this->settings->get('key_hash_algorithm', 'sha1'); } /** reverted: --- b/src/DrupalMemcacheConfig.php +++ /dev/null @@ -1,63 +0,0 @@ -get('memcache', array()); - } - - /** - * Returns a memcache setting. - * - * Settings can be set in settings.php in the $settings['memcache'] array and - * requested by this function. Settings should be used over configuration for - * read-only, possibly low bootstrap configuration that is environment - * specific. - * - * @param string $name - * The name of the setting to return. - * @param mixed $default - * (optional) The default value to use if this setting is not set. - * - * @return mixed - * The value of the setting, the provided default if not set. - */ - public static function get($name, $default = NULL) { - return isset(self::$settings[$name]) ? self::$settings[$name] : $default; - } - - /** - * Returns all the settings. This is only used for testing purposes. - * - * @return array - * All the settings. - */ - public static function getAll() { - return self::$settings; - } -} reverted: --- b/src/DrupalMemcacheFactory.php +++ a/src/DrupalMemcacheFactory.php @@ -7,6 +7,7 @@ namespace Drupal\memcache; +use Drupal\Core\Site\Settings; use Psr\Log\LogLevel; /** @@ -17,7 +18,7 @@ /** * The settings object. * + * @var \Drupal\Core\Site\Settings - * @var \Drupal\memcache\DrupalMemcacheConfig */ protected $settings; @@ -54,7 +55,7 @@ /** * */ + public function __construct(Settings $settings) { - public function __construct(DrupalMemcacheConfig $settings) { $this->settings = $settings; $this->initialize(); @@ -143,7 +144,7 @@ */ protected function initialize() { // If an extension is specified in settings.php, use that when available. + $preferred = $this->settings->get('memcache_extension', NULL); - $preferred = $this->settings->get('extension', NULL); if (isset($preferred) && class_exists($preferred)) { $this->extension = $preferred; } @@ -159,14 +160,14 @@ } // Values from settings.php + $this->memcacheServers = $this->settings->get('memcache_servers', array('127.0.0.1:11211' => 'default')); + $this->memcacheBins = $this->settings->get('memcache_bins', array('default' => 'default')); - $this->memcacheServers = $this->settings->get('servers', array('127.0.0.1:11211' => 'default')); - $this->memcacheBins = $this->settings->get('bins', array('default' => 'default')); // Indicate whether to connect to memcache using a persistent connection. // Note: this only affects the Memcache PECL extension, and does not affect // the Memcached PECL extension. For a detailed explanation see: // http://drupal.org/node/822316#comment-4427676 + $this->memcachePersistent = $this->settings->get('memcache_persistent', FALSE); - $this->memcachePersistent = $this->settings->get('persistent', FALSE); } /** reverted: --- b/src/DrupalMemcached.php +++ a/src/DrupalMemcached.php @@ -7,6 +7,8 @@ namespace Drupal\memcache; +use Drupal\Core\Site\Settings; + /** * Class DrupalMemcached. */ @@ -15,7 +17,7 @@ /** * {@inheritdoc} */ + public function __construct($bin, Settings $settings) { - public function __construct($bin, DrupalMemcacheConfig $settings) { parent::__construct($bin, $settings); $this->memcache = new \Memcached(); @@ -29,7 +31,7 @@ } // See README.txt for setting custom Memcache options when using the // memcached PECL extension. + foreach ($this->settings->get('memcache_options', array()) as $key => $value) { - foreach ($this->settings->get('options', array()) as $key => $value) { $this->memcache->setOption($key, $value); } } reverted: --- b/src/MemcacheBackend.php +++ a/src/MemcacheBackend.php @@ -7,6 +7,7 @@ namespace Drupal\memcache; +use Drupal\Core\Site\Settings; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Cache\CacheTagsChecksumInterface; @@ -48,7 +49,7 @@ /** * The Settings instance. * + * @var \Drupal\Core\Site\Settings - * @var \Drupal\memcache\DrupalMemcacheConfig */ protected $settings; @@ -68,12 +69,12 @@ * The memcache object. * @param \Drupal\Core\Lock\LockBackendInterface $lock * The lock backend. + * @param \Drupal\Core\Site\Settings $settings - * @param \Drupal\memcache\DrupalMemcacheConfig $settings * The settings instance. * @param \Drupal\Core\Cache\CacheTagsChecksumInterface $checksum_provider * The cache tags checksum service. */ + public function __construct($bin, DrupalMemcacheInterface $memcache, LockBackendInterface $lock, Settings $settings, CacheTagsChecksumInterface $checksum_provider) { - public function __construct($bin, DrupalMemcacheInterface $memcache, LockBackendInterface $lock, DrupalMemcacheConfig $settings, CacheTagsChecksumInterface $checksum_provider) { $this->bin = $bin; $this->memcache = $memcache; $this->lock = $lock; @@ -122,7 +123,7 @@ // If the memcache_stampede_protection variable is set, allow one // process to rebuild the cache entry while serving expired content to // the rest. + if ($this->settings->get('memcache_stampede_protection', FALSE)) { - if ($this->settings->get('stampede_protection', FALSE)) { // The process that acquires the lock will get a cache miss, all // others will get a cache hit. if (!$this->lock->acquire($lock_key, $this->settings->get('memcache_stampede_semaphore', 15))) { @@ -137,7 +138,7 @@ // On cache misses, attempt to avoid stampedes when the // memcache_stampede_protection variable is enabled. else { + if ($this->settings->get('memcache_stampede_protection', FALSE) && !$this->lock->acquire($lock_key, $this->settings->get('memcache_stampede_semaphore', 15))) { - if ($this->settings->get('stampede_protection', FALSE) && !$this->lock->acquire($lock_key, $this->settings->get('stampede_semaphore', 15))) { // Prevent any single request from waiting more than three times due to // stampede protection. By default this is a maximum total wait of 15 // seconds. This accounts for two possibilities - a cache and lock miss @@ -148,11 +149,11 @@ // information. Currently the limit will kick in for three waits of 25ms // or three waits of 5000ms. $this->lockCount++; + if ($this->lockCount <= $this->settings->get('memcache_stampede_wait_limit', 3)) { - if ($this->lockCount <= $this->settings->get('stampede_wait_limit', 3)) { // The memcache_stampede_semaphore variable was used in previous // releases of memcache, but the max_wait variable was not, so by // default divide the semaphore value by 3 (5 seconds). + $this->lock->wait($lock_key, $this->settings->get('memcache_stampede_wait_time', 5)); - $this->lock->wait($lock_key, $this->settings->get('stampede_wait_time', 5)); $cache = $this->get($cid); } } reverted: --- b/src/MemcacheBackendFactory.php +++ a/src/MemcacheBackendFactory.php @@ -7,6 +7,7 @@ namespace Drupal\memcache; +use Drupal\Core\Site\Settings; use Drupal\Core\Lock\LockBackendInterface; use Drupal\Core\Cache\CacheTagsChecksumInterface; @@ -25,7 +26,7 @@ /** * The settings object. * + * @var \Drupal\Core\Site\Settings - * @var \Drupal\memcache\DrupalMemcacheConfig */ protected $settings; @@ -47,10 +48,10 @@ * Constructs the DatabaseBackendFactory object. * * @param \Drupal\Core\Lock\LockBackendInterface $lock + * @param \Drupal\Core\Site\Settings $settings - * @param \Drupal\memcache\DrupalMemcacheConfig $settings * @param \Drupal\memcache\DrupalMemcacheFactory $memcache_factory */ + function __construct(LockBackendInterface $lock, Settings $settings, DrupalMemcacheFactory $memcache_factory, CacheTagsChecksumInterface $checksum_provider) { - function __construct(LockBackendInterface $lock, DrupalMemcacheConfig $settings, DrupalMemcacheFactory $memcache_factory, CacheTagsChecksumInterface $checksum_provider) { $this->lock = $lock; $this->settings = $settings; $this->memcacheFactory = $memcache_factory; reverted: --- b/src/Tests/DrupalMemcacheConfigTest.php +++ /dev/null @@ -1,77 +0,0 @@ -config = array( - 'memcache' => array( - 'servers' => array('127.0.0.2:12345' => 'default'), - 'bin' => array('default' => 'default') - ), - 'hash_salt' => $this->randomMachineName(), - ); - $settings = new Settings($this->config); - $this->settings = new DrupalMemcacheConfig($settings); - } - - /** - * @covers ::get - */ - public function testGet() { - // Test stored settings. - $this->assertEquals($this->config['memcache']['servers'], $this->settings->get('servers'), 'The correct setting was not returned.'); - $this->assertEquals($this->config['memcache']['bin'], $this->settings->get('bin'), 'The correct setting was not returned.'); - - // Test retrieving settings via static methods - $this->assertEquals($this->config['memcache']['servers'], DrupalMemcacheConfig::get('servers'), 'The correct setting was not returned.'); - $this->assertEquals($this->config['memcache']['bin'], DrupalMemcacheConfig::get('bin'), 'The correct setting was not returned.'); - - // Test setting that isn't stored with default. - $this->assertEquals('3', $this->settings->get('three', '3'), 'Default value for a setting not properly returned.'); - $this->assertNull($this->settings->get('nokey'), 'Non-null value returned for a setting that should not exist.'); - - // Test setting that isn't stored with default using static methods. - $this->assertEquals('4', DrupalMemcacheConfig::get('three', '4'), 'Default value for a setting not properly returned.'); - $this->assertNull(DrupalMemcacheConfig::get('nokey'), 'Non-null value returned for a setting that should not exist.'); - } - - /** - * @covers ::getAll - */ - public function testGetAll() { - $this->assertEquals($this->config['memcache'], $this->settings->getAll()); - $this->assertEquals($this->config['memcache'], DrupalMemcacheConfig::getAll()); - } -}