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

  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(private readonly Connection $connection, private readonly ?CacheItemPoolInterface $restartSignalCachePool null)
  16.     {
  17.     }
  18.     public function load(): string
  19.     {
  20.         $cacheId EnvironmentHelper::getVariable('SHOPWARE_CACHE_ID');
  21.         if ($cacheId) {
  22.             return (string) $cacheId;
  23.         }
  24.         try {
  25.             $cacheId $this->connection->fetchOne(
  26.                 '# cache-id-loader
  27.                 SELECT `value` FROM app_config WHERE `key` = :key',
  28.                 ['key' => 'cache-id']
  29.             );
  30.         } catch (\Exception) {
  31.             $cacheId null;
  32.         }
  33.         if (\is_string($cacheId)) {
  34.             return $cacheId;
  35.         }
  36.         $cacheId Uuid::randomHex();
  37.         try {
  38.             $this->write($cacheId);
  39.             return $cacheId;
  40.         } catch (\Exception) {
  41.             return 'live';
  42.         }
  43.     }
  44.     public function write(string $cacheId): void
  45.     {
  46.         $this->connection->executeStatement(
  47.             'REPLACE INTO app_config (`key`, `value`) VALUES (:key, :cacheId)',
  48.             ['cacheId' => $cacheId'key' => 'cache-id']
  49.         );
  50.         if ($this->restartSignalCachePool) {
  51.             $cacheItem $this->restartSignalCachePool->getItem(StopWorkerOnRestartSignalListener::RESTART_REQUESTED_TIMESTAMP_KEY);
  52.             $cacheItem->set(microtime(true));
  53.             $this->restartSignalCachePool->save($cacheItem);
  54.         }
  55.     }
  56. }