vendor/dvdw/cache/src/Framework/Adapter/Cache/CacheHelper.php line 58

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Dvdw\Cache\Framework\Adapter\Cache;
  3. use Dvdw\Cache\System\SalesChannel\AbstractStoreApiResponseTagBuilder;
  4. use Psr\Cache\CacheException;
  5. use Psr\Cache\InvalidArgumentException;
  6. use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
  7. use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
  9. use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\JsonFieldSerializer;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Core\System\SalesChannel\StoreApiResponse;
  13. use Symfony\Contracts\Cache\CacheInterface;
  14. use Symfony\Contracts\Cache\ItemInterface;
  15. class CacheHelper
  16. {
  17.     private CacheInterface $cache;
  18.     private EntityCacheKeyGenerator $generator;
  19.     private AbstractCacheTracer $tracer;
  20.     private AbstractStoreApiResponseTagBuilder $tagBuilder;
  21.     public function __construct(
  22.         CacheInterface $cache,
  23.         EntityCacheKeyGenerator $generator,
  24.         AbstractCacheTracer $tracer,
  25.         AbstractStoreApiResponseTagBuilder $tagBuilder
  26.     )
  27.     {
  28.         $this->cache $cache;
  29.         $this->generator $generator;
  30.         $this->tracer $tracer;
  31.         $this->tagBuilder $tagBuilder;
  32.     }
  33.     /**
  34.      * @throws CacheException
  35.      * @throws InvalidArgumentException
  36.      */
  37.     public function get(CacheItemInfo $itemInfo): StoreApiResponse
  38.     {
  39.         $key $itemInfo->getKey();
  40.         if ($key === null) {
  41.             $key $this->generateKey($itemInfo->buildName(), $itemInfo->getCriteria(), $itemInfo->getContext());
  42.         }
  43.         $value $this->cache->get($key, function (ItemInterface $item) use ($itemInfo) {
  44.             $name $itemInfo->buildName();
  45.             $response $this->tracer->trace($name, function () use ($itemInfo) {
  46.                 return $itemInfo->getRoute()->load($itemInfo->getRequest(), $itemInfo->getCriteria(), $itemInfo->getContext());
  47.             });
  48.             $tags $itemInfo->getTags();
  49.             if ($tags === null) {
  50.                 $tags = [];
  51.             }
  52.             $tags array_unique(array_filter(array_merge($tags$this->generateTags($response$itemInfo))));
  53.             $item->tag($tags);
  54.             return CacheValueCompressor::compress($response);
  55.         });
  56.         return CacheValueCompressor::uncompress($value);
  57.     }
  58.     private function generateKey(string $nameCriteria $criteriaSalesChannelContext $context): string
  59.     {
  60.         $parts = [
  61.             $this->generator->getCriteriaHash($criteria),
  62.             $this->generator->getSalesChannelContextHash($context)
  63.         ];
  64.         return $name '-' md5(JsonFieldSerializer::encodeJson($parts));
  65.     }
  66.     private function generateTags(StoreApiResponse $responseCacheItemInfo $itemInfo): array
  67.     {
  68.         $name $itemInfo->buildName();
  69.         $tags array_merge(
  70.             $this->tracer->get($name),
  71.             [$name$itemInfo->getAllTag()],
  72.             $this->tagBuilder->build($response$itemInfo)
  73.         );
  74.         return array_unique(array_filter($tags));
  75.     }
  76. }