ContractsTrait.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Traits;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\Cache\Adapter\AdapterInterface;
  13. use Symfony\Component\Cache\CacheItem;
  14. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  15. use Symfony\Component\Cache\LockRegistry;
  16. use Symfony\Contracts\Cache\CacheInterface;
  17. use Symfony\Contracts\Cache\CacheTrait;
  18. use Symfony\Contracts\Cache\ItemInterface;
  19. /**
  20. * @author Nicolas Grekas <p@tchwork.com>
  21. *
  22. * @internal
  23. */
  24. trait ContractsTrait
  25. {
  26. use CacheTrait {
  27. doGet as private contractsGet;
  28. }
  29. /**
  30. * @var callable
  31. */
  32. private $callbackWrapper;
  33. private array $computing = [];
  34. /**
  35. * Wraps the callback passed to ->get() in a callable.
  36. *
  37. * @return callable the previous callback wrapper
  38. */
  39. public function setCallbackWrapper(?callable $callbackWrapper): callable
  40. {
  41. if (!isset($this->callbackWrapper)) {
  42. $this->callbackWrapper = \Closure::fromCallable([LockRegistry::class, 'compute']);
  43. if (\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
  44. $this->setCallbackWrapper(null);
  45. }
  46. }
  47. if (null !== $callbackWrapper && !$callbackWrapper instanceof \Closure) {
  48. $callbackWrapper = \Closure::fromCallable($callbackWrapper);
  49. }
  50. $previousWrapper = $this->callbackWrapper;
  51. $this->callbackWrapper = $callbackWrapper ?? static function (callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata, ?LoggerInterface $logger) {
  52. return $callback($item, $save);
  53. };
  54. return $previousWrapper;
  55. }
  56. private function doGet(AdapterInterface $pool, string $key, callable $callback, ?float $beta, array &$metadata = null)
  57. {
  58. if (0 > $beta = $beta ?? 1.0) {
  59. throw new InvalidArgumentException(sprintf('Argument "$beta" provided to "%s::get()" must be a positive number, %f given.', static::class, $beta));
  60. }
  61. static $setMetadata;
  62. $setMetadata ?? $setMetadata = \Closure::bind(
  63. static function (CacheItem $item, float $startTime, ?array &$metadata) {
  64. if ($item->expiry > $endTime = microtime(true)) {
  65. $item->newMetadata[CacheItem::METADATA_EXPIRY] = $metadata[CacheItem::METADATA_EXPIRY] = $item->expiry;
  66. $item->newMetadata[CacheItem::METADATA_CTIME] = $metadata[CacheItem::METADATA_CTIME] = (int) ceil(1000 * ($endTime - $startTime));
  67. } else {
  68. unset($metadata[CacheItem::METADATA_EXPIRY], $metadata[CacheItem::METADATA_CTIME]);
  69. }
  70. },
  71. null,
  72. CacheItem::class
  73. );
  74. $this->callbackWrapper ??= \Closure::fromCallable([LockRegistry::class, 'compute']);
  75. return $this->contractsGet($pool, $key, function (CacheItem $item, bool &$save) use ($pool, $callback, $setMetadata, &$metadata, $key) {
  76. // don't wrap nor save recursive calls
  77. if (isset($this->computing[$key])) {
  78. $value = $callback($item, $save);
  79. $save = false;
  80. return $value;
  81. }
  82. $this->computing[$key] = $key;
  83. $startTime = microtime(true);
  84. if (!isset($this->callbackWrapper)) {
  85. $this->setCallbackWrapper($this->setCallbackWrapper(null));
  86. }
  87. try {
  88. $value = ($this->callbackWrapper)($callback, $item, $save, $pool, function (CacheItem $item) use ($setMetadata, $startTime, &$metadata) {
  89. $setMetadata($item, $startTime, $metadata);
  90. }, $this->logger ?? null);
  91. $setMetadata($item, $startTime, $metadata);
  92. return $value;
  93. } finally {
  94. unset($this->computing[$key]);
  95. }
  96. }, $beta, $metadata, $this->logger ?? null);
  97. }
  98. }