diff options
author | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
commit | bf6655a534a6775d30cafa67bd801276bda1d98d (patch) | |
tree | c6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/symfony/cache/Messenger | |
parent | 94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff) | |
download | AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip |
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/symfony/cache/Messenger')
3 files changed, 241 insertions, 0 deletions
diff --git a/vendor/symfony/cache/Messenger/EarlyExpirationDispatcher.php b/vendor/symfony/cache/Messenger/EarlyExpirationDispatcher.php new file mode 100644 index 0000000..d972634 --- /dev/null +++ b/vendor/symfony/cache/Messenger/EarlyExpirationDispatcher.php | |||
@@ -0,0 +1,60 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | namespace Symfony\Component\Cache\Messenger; | ||
13 | |||
14 | use Psr\Log\LoggerInterface; | ||
15 | use Symfony\Component\Cache\Adapter\AdapterInterface; | ||
16 | use Symfony\Component\Cache\CacheItem; | ||
17 | use Symfony\Component\DependencyInjection\ReverseContainer; | ||
18 | use Symfony\Component\Messenger\MessageBusInterface; | ||
19 | use Symfony\Component\Messenger\Stamp\HandledStamp; | ||
20 | |||
21 | /** | ||
22 | * Sends the computation of cached values to a message bus. | ||
23 | */ | ||
24 | class EarlyExpirationDispatcher | ||
25 | { | ||
26 | private ?\Closure $callbackWrapper; | ||
27 | |||
28 | public function __construct( | ||
29 | private MessageBusInterface $bus, | ||
30 | private ReverseContainer $reverseContainer, | ||
31 | ?callable $callbackWrapper = null, | ||
32 | ) { | ||
33 | $this->callbackWrapper = null === $callbackWrapper ? null : $callbackWrapper(...); | ||
34 | } | ||
35 | |||
36 | public function __invoke(callable $callback, CacheItem $item, bool &$save, AdapterInterface $pool, \Closure $setMetadata, ?LoggerInterface $logger = null): mixed | ||
37 | { | ||
38 | if (!$item->isHit() || null === $message = EarlyExpirationMessage::create($this->reverseContainer, $callback, $item, $pool)) { | ||
39 | // The item is stale or the callback cannot be reversed: we must compute the value now | ||
40 | $logger?->info('Computing item "{key}" online: '.($item->isHit() ? 'callback cannot be reversed' : 'item is stale'), ['key' => $item->getKey()]); | ||
41 | |||
42 | return null !== $this->callbackWrapper ? ($this->callbackWrapper)($callback, $item, $save, $pool, $setMetadata, $logger) : $callback($item, $save); | ||
43 | } | ||
44 | |||
45 | $envelope = $this->bus->dispatch($message); | ||
46 | |||
47 | if ($logger) { | ||
48 | if ($envelope->last(HandledStamp::class)) { | ||
49 | $logger->info('Item "{key}" was computed online', ['key' => $item->getKey()]); | ||
50 | } else { | ||
51 | $logger->info('Item "{key}" sent for recomputation', ['key' => $item->getKey()]); | ||
52 | } | ||
53 | } | ||
54 | |||
55 | // The item's value is not stale, no need to write it to the backend | ||
56 | $save = false; | ||
57 | |||
58 | return $message->getItem()->get() ?? $item->get(); | ||
59 | } | ||
60 | } | ||
diff --git a/vendor/symfony/cache/Messenger/EarlyExpirationHandler.php b/vendor/symfony/cache/Messenger/EarlyExpirationHandler.php new file mode 100644 index 0000000..adaeb5b --- /dev/null +++ b/vendor/symfony/cache/Messenger/EarlyExpirationHandler.php | |||
@@ -0,0 +1,81 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | namespace Symfony\Component\Cache\Messenger; | ||
13 | |||
14 | use Symfony\Component\Cache\CacheItem; | ||
15 | use Symfony\Component\DependencyInjection\ReverseContainer; | ||
16 | use Symfony\Component\Messenger\Attribute\AsMessageHandler; | ||
17 | |||
18 | /** | ||
19 | * Computes cached values sent to a message bus. | ||
20 | */ | ||
21 | #[AsMessageHandler] | ||
22 | class EarlyExpirationHandler | ||
23 | { | ||
24 | private array $processedNonces = []; | ||
25 | |||
26 | public function __construct( | ||
27 | private ReverseContainer $reverseContainer, | ||
28 | ) { | ||
29 | } | ||
30 | |||
31 | public function __invoke(EarlyExpirationMessage $message): void | ||
32 | { | ||
33 | $item = $message->getItem(); | ||
34 | $metadata = $item->getMetadata(); | ||
35 | $expiry = $metadata[CacheItem::METADATA_EXPIRY] ?? 0; | ||
36 | $ctime = $metadata[CacheItem::METADATA_CTIME] ?? 0; | ||
37 | |||
38 | if ($expiry && $ctime) { | ||
39 | // skip duplicate or expired messages | ||
40 | |||
41 | $processingNonce = [$expiry, $ctime]; | ||
42 | $pool = $message->getPool(); | ||
43 | $key = $item->getKey(); | ||
44 | |||
45 | if (($this->processedNonces[$pool][$key] ?? null) === $processingNonce) { | ||
46 | return; | ||
47 | } | ||
48 | |||
49 | if (microtime(true) >= $expiry) { | ||
50 | return; | ||
51 | } | ||
52 | |||
53 | $this->processedNonces[$pool] = [$key => $processingNonce] + ($this->processedNonces[$pool] ?? []); | ||
54 | |||
55 | if (\count($this->processedNonces[$pool]) > 100) { | ||
56 | array_pop($this->processedNonces[$pool]); | ||
57 | } | ||
58 | } | ||
59 | |||
60 | static $setMetadata; | ||
61 | |||
62 | $setMetadata ??= \Closure::bind( | ||
63 | function (CacheItem $item, float $startTime) { | ||
64 | if ($item->expiry > $endTime = microtime(true)) { | ||
65 | $item->newMetadata[CacheItem::METADATA_EXPIRY] = $item->expiry; | ||
66 | $item->newMetadata[CacheItem::METADATA_CTIME] = (int) ceil(1000 * ($endTime - $startTime)); | ||
67 | } | ||
68 | }, | ||
69 | null, | ||
70 | CacheItem::class | ||
71 | ); | ||
72 | |||
73 | $startTime = microtime(true); | ||
74 | $pool = $message->findPool($this->reverseContainer); | ||
75 | $callback = $message->findCallback($this->reverseContainer); | ||
76 | $save = true; | ||
77 | $value = $callback($item, $save); | ||
78 | $setMetadata($item, $startTime); | ||
79 | $pool->save($item->set($value)); | ||
80 | } | ||
81 | } | ||
diff --git a/vendor/symfony/cache/Messenger/EarlyExpirationMessage.php b/vendor/symfony/cache/Messenger/EarlyExpirationMessage.php new file mode 100644 index 0000000..6056eba --- /dev/null +++ b/vendor/symfony/cache/Messenger/EarlyExpirationMessage.php | |||
@@ -0,0 +1,100 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | namespace Symfony\Component\Cache\Messenger; | ||
13 | |||
14 | use Symfony\Component\Cache\Adapter\AdapterInterface; | ||
15 | use Symfony\Component\Cache\CacheItem; | ||
16 | use Symfony\Component\DependencyInjection\ReverseContainer; | ||
17 | |||
18 | /** | ||
19 | * Conveys a cached value that needs to be computed. | ||
20 | */ | ||
21 | final class EarlyExpirationMessage | ||
22 | { | ||
23 | private CacheItem $item; | ||
24 | private string $pool; | ||
25 | private string|array $callback; | ||
26 | |||
27 | public static function create(ReverseContainer $reverseContainer, callable $callback, CacheItem $item, AdapterInterface $pool): ?self | ||
28 | { | ||
29 | try { | ||
30 | $item = clone $item; | ||
31 | $item->set(null); | ||
32 | } catch (\Exception) { | ||
33 | return null; | ||
34 | } | ||
35 | |||
36 | $pool = $reverseContainer->getId($pool); | ||
37 | |||
38 | if (\is_object($callback)) { | ||
39 | if (null === $id = $reverseContainer->getId($callback)) { | ||
40 | return null; | ||
41 | } | ||
42 | |||
43 | $callback = '@'.$id; | ||
44 | } elseif (!\is_array($callback)) { | ||
45 | $callback = (string) $callback; | ||
46 | } elseif (!\is_object($callback[0])) { | ||
47 | $callback = [(string) $callback[0], (string) $callback[1]]; | ||
48 | } else { | ||
49 | if (null === $id = $reverseContainer->getId($callback[0])) { | ||
50 | return null; | ||
51 | } | ||
52 | |||
53 | $callback = ['@'.$id, (string) $callback[1]]; | ||
54 | } | ||
55 | |||
56 | return new self($item, $pool, $callback); | ||
57 | } | ||
58 | |||
59 | public function getItem(): CacheItem | ||
60 | { | ||
61 | return $this->item; | ||
62 | } | ||
63 | |||
64 | public function getPool(): string | ||
65 | { | ||
66 | return $this->pool; | ||
67 | } | ||
68 | |||
69 | /** | ||
70 | * @return string|string[] | ||
71 | */ | ||
72 | public function getCallback(): string|array | ||
73 | { | ||
74 | return $this->callback; | ||
75 | } | ||
76 | |||
77 | public function findPool(ReverseContainer $reverseContainer): AdapterInterface | ||
78 | { | ||
79 | return $reverseContainer->getService($this->pool); | ||
80 | } | ||
81 | |||
82 | public function findCallback(ReverseContainer $reverseContainer): callable | ||
83 | { | ||
84 | if (\is_string($callback = $this->callback)) { | ||
85 | return '@' === $callback[0] ? $reverseContainer->getService(substr($callback, 1)) : $callback; | ||
86 | } | ||
87 | if ('@' === $callback[0][0]) { | ||
88 | $callback[0] = $reverseContainer->getService(substr($callback[0], 1)); | ||
89 | } | ||
90 | |||
91 | return $callback; | ||
92 | } | ||
93 | |||
94 | private function __construct(CacheItem $item, string $pool, string|array $callback) | ||
95 | { | ||
96 | $this->item = $item; | ||
97 | $this->pool = $pool; | ||
98 | $this->callback = $callback; | ||
99 | } | ||
100 | } | ||