|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace BEAR\QueryRepository; |
| 6 | + |
| 7 | +use BEAR\QueryRepository\Log\Context\InvalidateContext; |
| 8 | +use BEAR\QueryRepository\Log\Context\ManualInvalidateContext; |
| 9 | +use BEAR\QueryRepository\Log\Context\SaveDonutContext; |
| 10 | +use BEAR\QueryRepository\Log\Context\SaveDonutViewContext; |
| 11 | +use BEAR\QueryRepository\Log\Context\SaveEtagContext; |
| 12 | +use BEAR\QueryRepository\Log\Context\SaveValueContext; |
| 13 | +use BEAR\QueryRepository\Log\Context\SaveViewContext; |
| 14 | +use BEAR\QueryRepository\Log\SafeSemanticLogger; |
| 15 | +use BEAR\Resource\AbstractUri; |
| 16 | +use BEAR\Resource\ResourceObject; |
| 17 | +use Koriym\SemanticLogger\SemanticLoggerInterface; |
| 18 | +use Override; |
| 19 | +use Ray\Di\Di\Named; |
| 20 | + |
| 21 | +use function hrtime; |
| 22 | +use function round; |
| 23 | + |
| 24 | +/** |
| 25 | + * Logging decorator for ResourceStorage |
| 26 | + * |
| 27 | + * Keeps the storage itself free of any logging concern: every cache write emits its |
| 28 | + * semantic-log event here, around a transparent delegation to the wrapped storage. |
| 29 | + * The tags reported for a save are recomputed with the same {@see CacheTags} the |
| 30 | + * storage uses, so the log records exactly what was stored. |
| 31 | + * |
| 32 | + * Reads (get / getDonut / hasEtag) are pass-through: a request's hit/miss is logged |
| 33 | + * at the interceptor / donut-repository layer where the request-level outcome lives, |
| 34 | + * not at the low-level pool read. |
| 35 | + */ |
| 36 | +final class LoggableResourceStorage implements ResourceStorageInterface |
| 37 | +{ |
| 38 | + public function __construct( |
| 39 | + #[Named('origin')] |
| 40 | + private readonly ResourceStorageInterface $storage, |
| 41 | + private readonly SemanticLoggerInterface $logger, |
| 42 | + private readonly CacheTags $cacheTags, |
| 43 | + private readonly UriTagInterface $uriTag, |
| 44 | + ) { |
| 45 | + } |
| 46 | + |
| 47 | + #[Override] |
| 48 | + public function hasEtag(string $etag): bool |
| 49 | + { |
| 50 | + return $this->storage->hasEtag($etag); |
| 51 | + } |
| 52 | + |
| 53 | + #[Override] |
| 54 | + public function saveEtag(AbstractUri $uri, string $etag, string $surrogateKeys, int|null $ttl): void |
| 55 | + { |
| 56 | + $this->storage->saveEtag($uri, $etag, $surrogateKeys, $ttl); |
| 57 | + $this->logger->event(new SaveEtagContext((string) $uri, $etag, $this->cacheTags->ofEtag($uri, $surrogateKeys))); |
| 58 | + } |
| 59 | + |
| 60 | + #[Override] |
| 61 | + public function deleteEtag(AbstractUri $uri) |
| 62 | + { |
| 63 | + return $this->invalidateTags([($this->uriTag)($uri)])->isInvalidated(); |
| 64 | + } |
| 65 | + |
| 66 | + #[Override] |
| 67 | + public function get(AbstractUri $uri): ResourceState|null |
| 68 | + { |
| 69 | + return $this->storage->get($uri); |
| 70 | + } |
| 71 | + |
| 72 | + #[Override] |
| 73 | + public function getDonut(AbstractUri $uri): ResourceDonut|null |
| 74 | + { |
| 75 | + return $this->storage->getDonut($uri); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * {@inheritDoc} |
| 80 | + * |
| 81 | + * A top-level invalidation is a direct (non-AOP) call with no enclosing scope, so the |
| 82 | + * event would be dropped at flush. Root it in a manual_invalidate scope whose close |
| 83 | + * carries the outcome. Nested invalidations (inside a GET or a command) stay events. |
| 84 | + * |
| 85 | + * The CDN purge is fail-closed: a purge failure is logged (cdn=failed) and then |
| 86 | + * re-thrown so a write does not silently leave stale CDN content. |
| 87 | + */ |
| 88 | + #[Override] |
| 89 | + public function invalidateTags(array $tags): InvalidateResult |
| 90 | + { |
| 91 | + $start = hrtime(true); |
| 92 | + $result = $this->storage->invalidateTags($tags); |
| 93 | + $context = new InvalidateContext( |
| 94 | + $result->tags, |
| 95 | + roPoolInvalidated: $result->roInvalidated, |
| 96 | + etagPoolInvalidated: $result->etagInvalidated, |
| 97 | + cdnPurged: $result->cdnError === null, |
| 98 | + durationMs: round((hrtime(true) - $start) / 1_000_000, 3), |
| 99 | + ); |
| 100 | + |
| 101 | + if ($this->logger instanceof SafeSemanticLogger && $this->logger->isTopLevel()) { |
| 102 | + $openId = $this->logger->open(new ManualInvalidateContext($tags)); |
| 103 | + $this->logger->close($context, $openId); |
| 104 | + } else { |
| 105 | + $this->logger->event($context); |
| 106 | + } |
| 107 | + |
| 108 | + if ($result->cdnError !== null) { |
| 109 | + throw $result->cdnError; |
| 110 | + } |
| 111 | + |
| 112 | + return $result; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * {@inheritDoc} |
| 117 | + * |
| 118 | + * @return bool |
| 119 | + */ |
| 120 | + #[Override] |
| 121 | + public function saveValue(ResourceObject $ro, int $ttl) |
| 122 | + { |
| 123 | + $saved = $this->storage->saveValue($ro, $ttl); |
| 124 | + $this->logger->event(new SaveValueContext((string) $ro->uri, $this->cacheTags->ofResource($ro), $ttl)); |
| 125 | + |
| 126 | + return $saved; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * {@inheritDoc} |
| 131 | + * |
| 132 | + * @return bool |
| 133 | + */ |
| 134 | + #[Override] |
| 135 | + public function saveView(ResourceObject $ro, int $ttl) |
| 136 | + { |
| 137 | + $saved = $this->storage->saveView($ro, $ttl); |
| 138 | + $this->logger->event(new SaveViewContext((string) $ro->uri, $ttl)); |
| 139 | + |
| 140 | + return $saved; |
| 141 | + } |
| 142 | + |
| 143 | + #[Override] |
| 144 | + public function saveDonut(AbstractUri $uri, ResourceDonut $donut, int|null $sMaxAge, array $headerKeys): void |
| 145 | + { |
| 146 | + $this->storage->saveDonut($uri, $donut, $sMaxAge, $headerKeys); |
| 147 | + $this->logger->event(new SaveDonutContext((string) $uri, $sMaxAge)); |
| 148 | + } |
| 149 | + |
| 150 | + #[Override] |
| 151 | + public function saveDonutView(ResourceObject $ro, int|null $ttl): bool |
| 152 | + { |
| 153 | + $saved = $this->storage->saveDonutView($ro, $ttl); |
| 154 | + $this->logger->event(new SaveDonutViewContext((string) $ro->uri, $this->cacheTags->ofResource($ro), $ttl)); |
| 155 | + |
| 156 | + return $saved; |
| 157 | + } |
| 158 | +} |
0 commit comments