39 lines
1.1 KiB
PHP
39 lines
1.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service;
|
|
|
|
use App\Entity\DTO\QRCode\QRCode;
|
|
use Exception;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Contracts\Cache\CacheInterface;
|
|
use Symfony\Contracts\Cache\ItemInterface;
|
|
|
|
|
|
final readonly class CachedQRCodeGenerator implements QRCodeGeneratorInterface
|
|
{
|
|
private \DateInterval $cacheDuration;
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function __construct(private CacheableQRCodeGeneratorInterface $innerGenerator, private CacheInterface $cache, private LoggerInterface $logger, string $cacheDuration)
|
|
{
|
|
$this->cacheDuration = new \DateInterval($cacheDuration);
|
|
}
|
|
|
|
public function generateQRCodeFromEntity(QRCode $entity): string
|
|
{
|
|
$key = $this->innerGenerator->getCacheKey($entity);
|
|
return $this->cache->get($key, function(ItemInterface $c) use ($entity) {
|
|
$this->logger->debug("cache miss for key " . $c->getKey(), [
|
|
'entity' => $entity,
|
|
]);
|
|
|
|
$c->expiresAfter($this->cacheDuration);
|
|
|
|
return $this->innerGenerator->generateQRCodeFromEntity($entity);
|
|
});
|
|
}
|
|
}
|