-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDonutRepository.php
More file actions
149 lines (126 loc) · 4.73 KB
/
DonutRepository.php
File metadata and controls
149 lines (126 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
declare(strict_types=1);
namespace BEAR\QueryRepository;
use BEAR\Resource\AbstractUri;
use BEAR\Resource\ResourceInterface;
use BEAR\Resource\ResourceObject;
use Override;
use function assert;
use function explode;
final readonly class DonutRepository implements DonutRepositoryInterface
{
public function __construct(
private QueryRepositoryInterface $queryRepository,
private HeaderSetter $headerSetter,
private ResourceStorageInterface $resourceStorage,
private ResourceInterface $resource,
private CdnCacheControlHeaderSetterInterface $cdnCacheControlHeaderSetter,
private RepositoryLoggerInterface $logger,
private DonutRendererInterface $renderer,
) {
}
#[Override]
public function get(ResourceObject $ro): ResourceObject|null
{
$maybeState = $this->queryRepository->get($ro->uri);
$this->logger->log('try-donut-view: uri:%s', $ro->uri);
if ($maybeState instanceof ResourceState) {
$this->logger->log('found-donut-view: uri:%s', $ro->uri);
$ro->headers = $maybeState->headers;
$ro->view = $maybeState->view;
return $ro;
}
return $this->refreshDonut($ro);
}
/**
* {@inheritDoc}
*/
#[Override]
public function putStatic(ResourceObject $ro, int|null $ttl = null, int|null $sMaxAge = null): ResourceObject
{
$this->logger->log('put-donut: uri:%s ttl:%s s-maxage:%d', (string) $ro->uri, $sMaxAge, $ttl);
$keys = new SurrogateKeys($ro->uri);
$keys->addTag($ro);
$headerKeys = $this->getHeaderKeys($ro);
$donut = ResourceDonut::create($ro, $this->renderer, $keys, $sMaxAge, true);
$donut->render($ro, $this->renderer);
$this->setHeaders($keys, $ro, $sMaxAge);
// delete
$this->resourceStorage->invalidateTags([(new UriTag())($ro->uri)]);
// save content cache and donut
$this->saveView($ro, $sMaxAge);
$this->resourceStorage->saveDonut($ro->uri, $donut, $ttl, $headerKeys);
return $ro;
}
/**
* {@inheritDoc}
*/
#[Override]
public function putDonut(ResourceObject $ro, int|null $donutTtl): ResourceObject
{
$this->logger->log('put-donut: uri:%s ttl:%s', (string) $ro->uri, $donutTtl);
$keys = new SurrogateKeys($ro->uri);
$keyArrays = $this->getHeaderKeys($ro);
$donut = ResourceDonut::create($ro, $this->renderer, $keys, $donutTtl, false);
$donut->render($ro, $this->renderer);
$keys->setSurrogateHeader($ro);
// delete
$this->resourceStorage->invalidateTags([(new UriTag())($ro->uri)]);
// save donut
$this->resourceStorage->saveDonut($ro->uri, $donut, $donutTtl, $keyArrays);
return $ro;
}
/**
* {@inheritDoc}
*/
#[Override]
public function purge(AbstractUri $uri): void
{
$this->queryRepository->purge($uri);
}
/**
* {@inheritDoc}
*/
#[Override]
public function invalidateTags(array $tags): void
{
$this->resourceStorage->invalidateTags($tags);
}
private function refreshDonut(ResourceObject $ro): ResourceObject|null
{
$donut = $this->resourceStorage->getDonut($ro->uri);
$this->logger->log('try-donut uri:%s', (string) $ro->uri);
if (! $donut instanceof ResourceDonut) {
$this->logger->log('no-donut-found uri:%s', (string) $ro->uri);
return null;
}
$this->logger->log('refresh-donut: uri:%s', $ro->uri);
$donut->refresh($this->resource, $ro);
if (! $donut->isCacheble) {
return $ro;
}
($this->headerSetter)($ro, $donut->ttl, null);
$ro->headers[Header::ETAG] .= 'r'; // mark refreshed by resource static
($this->cdnCacheControlHeaderSetter)($ro, $donut->ttl);
$this->saveView($ro, $donut->ttl);
return $ro;
}
private function saveView(ResourceObject $ro, int|null $ttl): bool
{
assert(isset($ro->headers[Header::ETAG]));
$surrogateKeys = $ro->headers[Header::SURROGATE_KEY] ?? '';
$this->resourceStorage->saveEtag($ro->uri, $ro->headers[Header::ETAG], $surrogateKeys, $ttl);
return $this->resourceStorage->saveDonutView($ro, $ttl);
}
private function setHeaders(SurrogateKeys $keys, ResourceObject $ro, int|null $sMaxAge): void
{
$keys->setSurrogateHeader($ro);
($this->cdnCacheControlHeaderSetter)($ro, $sMaxAge);
($this->headerSetter)($ro, 0, null);
}
/** @return list<string> */
public function getHeaderKeys(ResourceObject $ro): array
{
return isset($ro->headers[Header::SURROGATE_KEY]) ? explode(' ', $ro->headers[Header::SURROGATE_KEY]) : [];
}
}