addInvalidator example

$container = new Container();
    $container->set('cache.invalidator_cache_bin', $invalidator_cache_bin);
    $container->set('cache.non_invalidator_cache_bin', $non_invalidator_cache_bin);
    $container->setParameter('cache_bins', ['cache.invalidator_cache_bin' => 'invalidator_cache_bin', 'cache.non_invalidator_cache_bin' => 'non_invalidator_cache_bin']);
    $cache_tags_invalidator->setContainer($container);

    $invalidator = $this->createMock('\Drupal\Core\Cache\CacheTagsInvalidator');
    $invalidator->expects($this->once())
      ->method('invalidateTags')
      ->with(['node:1']);

    $cache_tags_invalidator->addInvalidator($invalidator);

    $cache_tags_invalidator->invalidateTags(['node:1']);
  }

}

  protected function createCacheBackend($bin) {
    $consistent_backend = new DatabaseBackend(\Drupal::service('database'), \Drupal::service('cache_tags.invalidator.checksum')$bin, 100);
    $fast_backend = new PhpBackend($bin, \Drupal::service('cache_tags.invalidator.checksum'));
    $backend = new ChainedFastBackend($consistent_backend$fast_backend$bin);
    // Explicitly register the cache bin as it can not work through the     // cache bin list in the container.     \Drupal::service('cache_tags.invalidator')->addInvalidator($backend);
    return $backend;
  }

}

class MemoryBackendTest extends GenericCacheBackendUnitTestBase {

  /** * Creates a new instance of MemoryBackend. * * @return \Drupal\Core\Cache\CacheBackendInterface * A new MemoryBackend object. */
  protected function createCacheBackend($bin) {
    $backend = new MemoryBackend();
    \Drupal::service('cache_tags.invalidator')->addInvalidator($backend);
    return $backend;
  }

}
class BackendChainTest extends GenericCacheBackendUnitTestBase {

  protected function createCacheBackend($bin) {
    $chain = new BackendChain();

    // We need to create some various backends in the chain.     $chain
      ->appendBackend(new MemoryBackend())
      ->prependBackend(new MemoryBackend())
      ->appendBackend(new MemoryBackend());

    \Drupal::service('cache_tags.invalidator')->addInvalidator($chain);

    return $chain;
  }

}
Home | Imprint | This part of the site doesn't use cookies.