ContractsTrait.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. private \Closure $callbackWrapper;
  30. private array $computing = [];
  31. /**
  32. * Wraps the callback passed to ->get() in a callable.
  33. *
  34. * @return callable the previous callback wrapper
  35. */
  36. public function setCallbackWrapper(?callable $callbackWrapper): callable
  37. {
  38. if (!isset($this->callbackWrapper)) {
  39. $this->callbackWrapper = \Closure::fromCallable([LockRegistry::class, 'compute']);
  40. if (\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
  41. $this->setCallbackWrapper(null);
  42. }
  43. }
  44. if (null !== $callbackWrapper && !$callbackWrapper instanceof \Closure) {
  45. $callbackWrapper = \Closure::fromCallable($callbackWrapper);
  46. }
  47. $previousWrapper = $this->callbackWrapper;
  48. $this->callbackWrapper = $callbackWrapper ?? static function (callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata, ?LoggerInterface $logger) {
  49. return $callback($item, $save);
  50. };
  51. return $previousWrapper;
  52. }
  53. private function doGet(AdapterInterface $pool, string $key, callable $callback, ?float $beta, array &$metadata = null)
  54. {
  55. if (0 > $beta = $beta ?? 1.0) {
  56. throw new InvalidArgumentException(sprintf('Argument "$beta" provided to "%s::get()" must be a positive number, %f given.', static::class, $beta));
  57. }
  58. static $setMetadata;
  59. $setMetadata ?? $setMetadata = \Closure::bind(
  60. static function (CacheItem $item, float $startTime, ?array &$metadata) {
  61. if ($item->expiry > $endTime = microtime(true)) {
  62. $item->newMetadata[CacheItem::METADATA_EXPIRY] = $metadata[CacheItem::METADATA_EXPIRY] = $item->expiry;
  63. $item->newMetadata[CacheItem::METADATA_CTIME] = $metadata[CacheItem::METADATA_CTIME] = (int) ceil(1000 * ($endTime - $startTime));
  64. } else {
  65. unset($metadata[CacheItem::METADATA_EXPIRY], $metadata[CacheItem::METADATA_CTIME]);
  66. }
  67. },
  68. null,
  69. CacheItem::class
  70. );
  71. $this->callbackWrapper ??= \Closure::fromCallable([LockRegistry::class, 'compute']);
  72. return $this->contractsGet($pool, $key, function (CacheItem $item, bool &$save) use ($pool, $callback, $setMetadata, &$metadata, $key) {
  73. // don't wrap nor save recursive calls
  74. if (isset($this->computing[$key])) {
  75. $value = $callback($item, $save);
  76. $save = false;
  77. return $value;
  78. }
  79. $this->computing[$key] = $key;
  80. $startTime = microtime(true);
  81. if (!isset($this->callbackWrapper)) {
  82. $this->setCallbackWrapper($this->setCallbackWrapper(null));
  83. }
  84. try {
  85. $value = ($this->callbackWrapper)($callback, $item, $save, $pool, function (CacheItem $item) use ($setMetadata, $startTime, &$metadata) {
  86. $setMetadata($item, $startTime, $metadata);
  87. }, $this->logger ?? null);
  88. $setMetadata($item, $startTime, $metadata);
  89. return $value;
  90. } finally {
  91. unset($this->computing[$key]);
  92. }
  93. }, $beta, $metadata, $this->logger ?? null);
  94. }
  95. }