custom/plugins/SwagEnterpriseSearchPlatform/src/Cache/CacheInvalidationSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Swag\EnterpriseSearch\Cache;
  4. use Psr\Cache\CacheItemPoolInterface;
  5. use Swag\EnterpriseSearch\Action\ActionSearchTerm\Events\ActionSearchTermIndexerEvent;
  6. use Swag\EnterpriseSearch\Action\Events\ActionIndexerEvent;
  7. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class CacheInvalidationSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var CacheItemPoolInterface[]
  13.      */
  14.     private $adapters;
  15.     /**
  16.      * @var \Shopware\Core\Framework\Adapter\Cache\CacheInvalidator|null
  17.      */
  18.     private $cacheInvalidator;
  19.     /**
  20.      * @param \Shopware\Core\Framework\Adapter\Cache\CacheInvalidator|null $cacheInvalidator
  21.      */
  22.     public function __construct(array $adapters$cacheInvalidator)
  23.     {
  24.         $this->adapters $adapters;
  25.         $this->cacheInvalidator $cacheInvalidator;
  26.     }
  27.     /**
  28.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  29.      */
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             ActionIndexerEvent::class => [
  34.                 ['invalidateSearch'2000],
  35.             ],
  36.             ActionSearchTermIndexerEvent::class => [
  37.                 ['invalidateSearch'2000],
  38.             ],
  39.         ];
  40.     }
  41.     // invalidates the search and suggest route each time a term changed
  42.     public function invalidateSearch(): void
  43.     {
  44.         $tags = [
  45.             'product-suggest-route',
  46.             'product-search-route',
  47.         ];
  48.         if ($this->cacheInvalidator === null) {
  49.             $this->invalidateCache($tags);
  50.             return;
  51.         }
  52.         $this->cacheInvalidator->invalidate($tags);
  53.     }
  54.     private function invalidateCache(array $keys): void
  55.     {
  56.         foreach ($this->adapters as $adapter) {
  57.             if ($adapter instanceof TagAwareAdapterInterface) {
  58.                 $adapter->invalidateTags($keys);
  59.             }
  60.         }
  61.         foreach ($this->adapters as $adapter) {
  62.             $adapter->deleteItems($keys);
  63.         }
  64.     }
  65. }