vendor/shopware/core/Framework/DataAbstractionLayer/Cache/CachedEntitySearcher.php line 59

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer\Cache;
  3. use Shopware\Core\Framework\App\AppDefinition;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearcherInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Version\Aggregate\VersionCommit\VersionCommitDefinition;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Version\Aggregate\VersionCommitData\VersionCommitDataDefinition;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Version\VersionDefinition;
  12. use Shopware\Core\Framework\Log\LogEntryDefinition;
  13. use Shopware\Core\Framework\MessageQueue\MessageQueueStatsDefinition;
  14. use Shopware\Core\Framework\Plugin\PluginDefinition;
  15. use Shopware\Core\System\NumberRange\Aggregate\NumberRangeState\NumberRangeStateDefinition;
  16. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  17. use Symfony\Component\Cache\CacheItem;
  18. class CachedEntitySearcher implements EntitySearcherInterface
  19. {
  20.     public const BLACKLIST = [
  21.         VersionDefinition::class,
  22.         VersionCommitDefinition::class,
  23.         VersionCommitDataDefinition::class,
  24.         NumberRangeStateDefinition::class,
  25.         PluginDefinition::class,
  26.         LogEntryDefinition::class,
  27.         MessageQueueStatsDefinition::class,
  28.         AppDefinition::class,
  29.     ];
  30.     /**
  31.      * @var EntityCacheKeyGenerator
  32.      */
  33.     private $cacheKeyGenerator;
  34.     /**
  35.      * @var TagAwareAdapterInterface
  36.      */
  37.     private $cache;
  38.     /**
  39.      * @var EntitySearcherInterface
  40.      */
  41.     private $decorated;
  42.     public function __construct(
  43.         EntityCacheKeyGenerator $cacheKeyGenerator,
  44.         TagAwareAdapterInterface $cache,
  45.         EntitySearcherInterface $decorated
  46.     ) {
  47.         $this->cacheKeyGenerator $cacheKeyGenerator;
  48.         $this->cache $cache;
  49.         $this->decorated $decorated;
  50.     }
  51.     public function search(EntityDefinition $definitionCriteria $criteriaContext $context): IdSearchResult
  52.     {
  53.         if (!$context->getUseCache()) {
  54.             return $this->decorated->search($definition$criteria$context);
  55.         }
  56.         if (\in_array($definition->getClass(), self::BLACKLISTtrue)) {
  57.             return $this->decorated->search($definition$criteria$context);
  58.         }
  59.         $key $this->cacheKeyGenerator->getSearchCacheKey($definition$criteria$context);
  60.         $item $this->cache->getItem($key);
  61.         if ($item->isHit()) {
  62.             return $item->get();
  63.         }
  64.         $result $this->decorated->search($definition$criteria$context);
  65.         $item->set($result);
  66.         $tags $this->cacheKeyGenerator->getSearchTags($definition$criteria);
  67.         if (!$item instanceof CacheItem) {
  68.             throw new \RuntimeException(sprintf('Cache adapter has to return instance of %s'CacheItem::class));
  69.         }
  70.         /* @var CacheItem $item */
  71.         $item->tag($tags);
  72.         $this->cache->save($item);
  73.         return $result;
  74.     }
  75. }