ContractsTrait.php 3.8 KB

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