custom/plugins/ApplifactionGuidedShopping/src/Service/CacheService.php line 63

Open in your IDE?
  1. <?php
  2. namespace ApplifactionGuidedShopping\Service;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\Exception;
  5. use Shopware\Core\Defaults;
  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\Field\Flag\RuleAreas;
  10. use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\JsonFieldSerializer;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Symfony\Contracts\Cache\CacheInterface;
  14. use Symfony\Contracts\Cache\ItemInterface;
  15. class CacheService
  16. {
  17. /**
  18. * @var CacheInterface
  19. */
  20. private $cache;
  21. /**
  22. * @var EntityCacheKeyGenerator
  23. */
  24. private $generator;
  25. /**
  26. * @var AbstractCacheTracer
  27. */
  28. private $tracer;
  29. /**
  30. * @var Connection
  31. */
  32. private $connection;
  33. public function __construct(
  34. CacheInterface $cache,
  35. EntityCacheKeyGenerator $generator,
  36. AbstractCacheTracer $tracer,
  37. Connection $connection
  38. )
  39. {
  40. $this->cache = $cache;
  41. $this->generator = $generator;
  42. $this->tracer = $tracer;
  43. $this->connection = $connection;
  44. }
  45. public function getCacheValueOrRecalculate(
  46. string $cacheKeyPrefix,
  47. array $cacheKeyData,
  48. \DateInterval $cacheExpiresAfter,
  49. \Closure $recalculateCallback,
  50. ?SalesChannelContext $context = null,
  51. ?Criteria $criteria = null): mixed
  52. {
  53. $cacheKey = $this->generateKey($cacheKeyPrefix, $cacheKeyData, $context, $criteria);
  54. $cacheResult = $this->cache->get($cacheKey, function (ItemInterface $item) use ($cacheKey, $cacheExpiresAfter, $recalculateCallback) {
  55. $traceResult = $this->tracer->trace($cacheKey, function () use ($recalculateCallback, $cacheExpiresAfter) {
  56. return $recalculateCallback();
  57. });
  58. $item->expiresAfter($cacheExpiresAfter);
  59. return CacheValueCompressor::compress($traceResult);
  60. });
  61. return CacheValueCompressor::uncompress($cacheResult);
  62. }
  63. private function generateKey(string $prefix, array $data = [], ?SalesChannelContext $context = null, ?Criteria $criteria = null): string
  64. {
  65. $parts = [];
  66. if (!empty($data)) {
  67. $parts[] = md5(JsonFieldSerializer::encodeJson($data));
  68. }
  69. if (!!$criteria) {
  70. $parts[] = $this->generator->getCriteriaHash($criteria);
  71. }
  72. if (!!$context) {
  73. $parts[] = $this->generator->getSalesChannelContextHash($context, [RuleAreas::PRODUCT_AREA]);
  74. }
  75. return $prefix . '-' . md5(JsonFieldSerializer::encodeJson($parts));
  76. }
  77. public function fetchUpdatedAtByProductIds(array $productIds): string
  78. {
  79. $updatedAtSql = <<<SQL
  80. SELECT created_at, updated_at
  81. FROM product
  82. WHERE LOWER(HEX(id)) IN (:productIds)
  83. SQL;
  84. try {
  85. $updatedAtResults = $this->connection->fetchAllAssociative(
  86. $updatedAtSql,
  87. ['productIds' => $productIds],
  88. ['productIds' => Connection::PARAM_STR_ARRAY]
  89. );
  90. } catch (\Exception $e) {
  91. return "";
  92. }
  93. $updatedAtResults = array_map(fn($updatedAtResult) => ($updatedAtResult['updated_at'] ?? $updatedAtResult['created_at']), $updatedAtResults);
  94. return join(';', $updatedAtResults);
  95. }
  96. public function fetchUpdatedAtByRuleIds(array $ruleIds): string
  97. {
  98. $updatedAtSql = <<<SQL
  99. SELECT created_at, updated_at
  100. FROM ags_accessory_rule
  101. WHERE LOWER(HEX(id)) IN (:ruleIds)
  102. SQL;
  103. try {
  104. $updatedAtResults = $this->connection->fetchAllAssociative(
  105. $updatedAtSql,
  106. ['ruleIds' => $ruleIds],
  107. ['ruleIds' => Connection::PARAM_STR_ARRAY]
  108. );
  109. } catch (\Exception $e) {
  110. return "";
  111. }
  112. $updatedAtResults = array_map(fn($updatedAtResult) => ($updatedAtResult['updated_at'] ?? $updatedAtResult['created_at']), $updatedAtResults);
  113. return join(';', $updatedAtResults);
  114. }
  115. public function fetchUpdatedAtByProductFinderIds(array $productFinderIds): string
  116. {
  117. $updatedAtSql = <<<SQL
  118. SELECT created_at, updated_at
  119. FROM ags_product_finder
  120. WHERE LOWER(HEX(id)) IN (:productFinderIds)
  121. SQL;
  122. try {
  123. $updatedAtResults = $this->connection->fetchAllAssociative(
  124. $updatedAtSql,
  125. ['productFinderIds' => $productFinderIds],
  126. ['productFinderIds' => Connection::PARAM_STR_ARRAY]
  127. );
  128. } catch (\Exception $e) {
  129. return "";
  130. }
  131. $updatedAtResults = array_map(fn($updatedAtResult) => ($updatedAtResult['updated_at'] ?? $updatedAtResult['created_at']), $updatedAtResults);
  132. return join(';', $updatedAtResults);
  133. }
  134. }