Hi, now on Drupal core the cache.container is using the database cache also when you have set it on the cache.container, the solution around this is set directly on $settings['bootstrap_container_definition'] definition, but now the DrupalMemcacheFactory don't have a static method that we can use to get the DrupalBackEnd instance...
$loader = new \Composer\Autoload\ClassLoader();
$loader->addPsr4('Drupal\\memcache\\', 'modules/contrib/memcache/src');
$loader->register();
$settings['bootstrap_container_definition'] = [
'parameters' => [],
'services' => [
'database' => [
'class' => 'Drupal\Core\Database\Connection',
'factory' => 'Drupal\Core\Database\Database::getConnection',
'arguments' => ['default'],
],
'settings.container' => [
'class' => 'Drupal\Core\Site\Settings',
'factory' => 'Drupal\Core\Site\Settings::getInstance',
],
'memcache.config.container' => [
'class' => 'Drupal\memcache\DrupalMemcacheConfig',
'arguments' => ['@settings.container'],
],
'memcache.backend.cache.container' => [
'class' => 'Drupal\memcache\DrupalMemcacheFactory',
'factory' => 'Drupal\memcache\DrupalMemcacheFactory::getInstance',
'arguments' => ['@memcache.config.container'],
],
'lock.container' => [
'class' => 'Drupal\memcache\Lock\MemcacheLockBackend',
'arguments' => ['container', '@memcache.backend.cache.container'],
],
'cache.container' => [
'class' => 'Drupal\memcache\MemcacheBackend',
'arguments' => ['container', '@memcache.backend.cache.container', '@lock.container', '@memcache.config.container', '@cache_tags_provider.container'],
],
'cache_tags_provider.container' => [
'class' => 'Drupal\Core\Cache\DatabaseCacheTagsChecksum',
'arguments' => ['@database'],
],
]
];
This should be the code to override it, but we don't have that static method. My suggestion is to create the static method inside the DrupalMemcacheFactory class, What you guys think about it?
Which that we can just override the cache container using memcache
The code for the static method should be like this
/**
* Get the right memcache drupal class.
*
* @param \Drupal\memcache\DrupalMemcacheConfig $settings
* @return \Drupal\memcache\DrupalMemcache|\Drupal\memcache\DrupalMemcached
*/
public static function getInstance(DrupalMemcacheConfig $settings) {
$preferred = $settings->get('extension', NULL);
if (isset($preferred) && class_exists($preferred)) {
$extension= $preferred;
}
// If no extension is set, default to Memcache. The Memcached extension has
// some features that the older extension lacks but also an unfixed bug that
// affects cache clears.
// @see http://pecl.php.net/bugs/bug.php?id=16829
elseif (class_exists('Memcache')) {
$extension = 'Memcache';
}
elseif (class_exists('Memcached')) {
$extension = 'Memcached';
}
else {
throw new MemcacheException('No Memcache extension found');
}
// Create a new Memcache object. Each cluster gets its own Memcache
// object.
// @todo Can't add a custom memcache class here yet.
if ($extension == 'Memcached') {
return new DrupalMemcached($settings);
}
return new DrupalMemcache($settings);
}
I cannot instantiate the class directly because I need to make the decision between the Memcache or memcached...
Comment | File | Size | Author |
---|---|---|---|
#11 | memcache-documentation_how_to_override_cache_container-2882755-11-d8.patch | 2.47 KB | guilopes |
Comments
Comment #2
guilopes CreditAttribution: guilopes at Media.Monks commentedA better solution for the method would be something like this
Comment #3
guilopes CreditAttribution: guilopes at Media.Monks commentedI've found a best way to do that
Comment #4
guilopes CreditAttribution: guilopes at Media.Monks commentedthe following patch add the documentation to the module. with this configuration you have no cache tables in the database anymore
Comment #5
guilopes CreditAttribution: guilopes at Media.Monks commentedComment #6
guilopes CreditAttribution: guilopes as a volunteer commentedis it possible to add this documentation? it's a non code change
Comment #7
mxh@guilopes Nice, many thanks for this!
I have tested your approach and it works. Your documentation is currently missing the class loading part, please add this to the documentation.
Please note: You should always be able to have a
$class_loader
variable being usable inside settings.php, see also Settings::initialize. I'm currently using this in my settings.php:Comment #8
guilopes CreditAttribution: guilopes as a volunteer commentedThanks @mxh, I've updated it on the following patch ;)
Comment #9
guilopes CreditAttribution: guilopes as a volunteer commentedcan me merge this documentation ?
Comment #10
damiankloip CreditAttribution: damiankloip commentedThis looks good! Just a couple of things:
s/starts/start
Also, this is way above 80 chars, can you split this text into multiple lines with limit of 80 chars per line please?
how come we need this check? does code from settings get loaded multiple times or something?
Comment #11
guilopes CreditAttribution: guilopes as a volunteer commentedThanks @damiankloip, it's updated
Comment #12
afi13 CreditAttribution: afi13 commentedOk for me
Comment #14
damiankloip CreditAttribution: damiankloip commentedComment #16
ericpughWould the new code block in README.txt replace the default settings?
$settings['cache']['default'] = 'cache.backend.memcache';
I'm getting a fatal error 'cache.backend.memcache' service not found.
Comment #17
guilopes CreditAttribution: guilopes as a volunteer commented@ericpught if you doesn't have the memcache module enabled you should include the memcache.services.yml as well