summaryrefslogtreecommitdiff
path: root/vendor/symfony/cache/Messenger/EarlyExpirationDispatcher.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/cache/Messenger/EarlyExpirationDispatcher.php')
-rw-r--r--vendor/symfony/cache/Messenger/EarlyExpirationDispatcher.php60
1 files changed, 60 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
12namespace Symfony\Component\Cache\Messenger;
13
14use Psr\Log\LoggerInterface;
15use Symfony\Component\Cache\Adapter\AdapterInterface;
16use Symfony\Component\Cache\CacheItem;
17use Symfony\Component\DependencyInjection\ReverseContainer;
18use Symfony\Component\Messenger\MessageBusInterface;
19use Symfony\Component\Messenger\Stamp\HandledStamp;
20
21/**
22 * Sends the computation of cached values to a message bus.
23 */
24class 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}