vendor/shopware/core/Framework/Adapter/Cache/CacheIdLoader.php line 35

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Adapter\Cache;
  3. use Doctrine\DBAL\Connection;
  4. use Psr\Cache\CacheItemPoolInterface;
  5. use Shopware\Core\DevOps\Environment\EnvironmentHelper;
  6. use Shopware\Core\Framework\Log\Package;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  8. use Symfony\Component\Messenger\EventListener\StopWorkerOnRestartSignalListener;
  9. #[Package('core')]
  10. class CacheIdLoader
  11. {
  12.     /**
  13.      * @internal
  14.      */
  15.     public function __construct(
  16.         private readonly Connection $connection,
  17.         private readonly ?CacheItemPoolInterface $restartSignalCachePool null
  18.     ) {
  19.     }
  20.     public function load(): string
  21.     {
  22.         $cacheId EnvironmentHelper::getVariable('SHOPWARE_CACHE_ID');
  23.         if ($cacheId) {
  24.             return (string) $cacheId;
  25.         }
  26.         try {
  27.             $cacheId $this->connection->fetchOne(
  28.                 '# cache-id-loader
  29.                 SELECT `value` FROM app_config WHERE `key` = :key',
  30.                 ['key' => 'cache-id']
  31.             );
  32.         } catch (\Exception) {
  33.             $cacheId null;
  34.         }
  35.         if (\is_string($cacheId)) {
  36.             return $cacheId;
  37.         }
  38.         $cacheId Uuid::randomHex();
  39.         try {
  40.             $this->write($cacheId);
  41.             return $cacheId;
  42.         } catch (\Exception) {
  43.             return 'live';
  44.         }
  45.     }
  46.     public function write(string $cacheId): void
  47.     {
  48.         $this->connection->executeStatement(
  49.             'REPLACE INTO app_config (`key`, `value`) VALUES (:key, :cacheId)',
  50.             ['cacheId' => $cacheId'key' => 'cache-id']
  51.         );
  52.         if ($this->restartSignalCachePool) {
  53.             $cacheItem $this->restartSignalCachePool->getItem(StopWorkerOnRestartSignalListener::RESTART_REQUESTED_TIMESTAMP_KEY);
  54.             $cacheItem->set(microtime(true));
  55.             $this->restartSignalCachePool->save($cacheItem);
  56.         }
  57.     }
  58. }