ContractsTrait.php 3.4 KB

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