3 namespace Drupal\Core\Cache;
5 use Drupal\Component\Assertion\Inspector;
6 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
9 * Passes cache tag events to classes that wish to respond to them.
11 class CacheTagsInvalidator implements CacheTagsInvalidatorInterface {
13 use ContainerAwareTrait;
16 * Holds an array of cache tags invalidators.
18 * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface[]
20 protected $invalidators = [];
25 public function invalidateTags(array $tags) {
26 assert(Inspector::assertAllStrings($tags), 'Cache tags must be strings.');
28 // Notify all added cache tags invalidators.
29 foreach ($this->invalidators as $invalidator) {
30 $invalidator->invalidateTags($tags);
33 // Additionally, notify each cache bin if it implements the service.
34 foreach ($this->getInvalidatorCacheBins() as $bin) {
35 $bin->invalidateTags($tags);
40 * Reset statically cached tags in all cache tag checksum services.
42 * This is only used by tests.
44 public function resetChecksums() {
45 foreach ($this->invalidators as $invalidator) {
46 if ($invalidator instanceof CacheTagsChecksumInterface) {
47 $invalidator->reset();
53 * Adds a cache tags invalidator.
55 * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $invalidator
56 * A cache invalidator.
58 public function addInvalidator(CacheTagsInvalidatorInterface $invalidator) {
59 $this->invalidators[] = $invalidator;
63 * Returns all cache bins that need to be notified about invalidations.
65 * @return \Drupal\Core\Cache\CacheTagsInvalidatorInterface[]
66 * An array of cache backend objects that implement the invalidator
67 * interface, keyed by their cache bin.
69 protected function getInvalidatorCacheBins() {
71 foreach ($this->container->getParameter('cache_bins') as $service_id => $bin) {
72 $service = $this->container->get($service_id);
73 if ($service instanceof CacheTagsInvalidatorInterface) {
74 $bins[$bin] = $service;