Skip to content

Commit

Permalink
Merge pull request #118 from koriym/spike
Browse files Browse the repository at this point in the history
Added surrogate key to Donut view
  • Loading branch information
koriym committed Jul 13, 2022
2 parents d9db550 + 0110dbb commit 434f1d3
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 9 deletions.
15 changes: 13 additions & 2 deletions src/DonutRepository.php
Expand Up @@ -9,6 +9,7 @@
use BEAR\Resource\ResourceObject;

use function assert;
use function explode;

final class DonutRepository implements DonutRepositoryInterface
{
Expand Down Expand Up @@ -60,14 +61,15 @@ public function putStatic(ResourceObject $ro, ?int $ttl = null, ?int $sMaxAge =
{
$this->logger->log('put-donut: uri:%s ttl:%s s-maxage:%d', (string) $ro->uri, $sMaxAge, $ttl);
$keys = new SurrogateKeys($ro->uri);
$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);
$this->resourceStorage->saveDonut($ro->uri, $donut, $ttl, $headerKeys);

return $ro;
}
Expand All @@ -79,13 +81,14 @@ public function putDonut(ResourceObject $ro, ?int $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);
$this->resourceStorage->saveDonut($ro->uri, $donut, $donutTtl, $keyArrays);

return $ro;
}
Expand Down Expand Up @@ -145,4 +148,12 @@ private function setHeaders(SurrogateKeys $keys, ResourceObject $ro, ?int $sMaxA
($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]) : [];
}
}
9 changes: 9 additions & 0 deletions src/Exception/ResourceStorageUnserializeException.php
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace BEAR\QueryRepository\Exception;

class ResourceStorageUnserializeException extends RuntimeException
{
}
8 changes: 5 additions & 3 deletions src/ResourceStorage.php
Expand Up @@ -191,12 +191,14 @@ public function saveView(ResourceObject $ro, int $ttl)
return $this->saver->__invoke($key, $value, $this->roPool, $tags, $ttl);
}

public function saveDonut(AbstractUri $uri, ResourceDonut $donut, ?int $sMaxAge): void
/**
* {@inheritDoc}
*/
public function saveDonut(AbstractUri $uri, ResourceDonut $donut, ?int $sMaxAge, array $headerKeys): void
{
$key = $this->getUriKey($uri, self::KEY_DONUT);
$this->logger->log('save-donut uri:%s s-maxage:%s', $uri, $sMaxAge);

$this->saver->__invoke($key, $donut, $this->roPool, [], $sMaxAge);
$this->saver->__invoke($key, $donut, $this->roPool, $headerKeys, $sMaxAge);
}

public function saveDonutView(ResourceObject $ro, ?int $ttl): bool
Expand Down
26 changes: 25 additions & 1 deletion src/ResourceStorageCacheableTrait.php
Expand Up @@ -4,9 +4,12 @@

namespace BEAR\QueryRepository;

use BEAR\QueryRepository\Exception\ResourceStorageUnserializeException;
use BEAR\RepositoryModule\Annotation\EtagPool;
use Error;
use Psr\Cache\CacheItemPoolInterface;
use Ray\Di\Di\Inject;
use Ray\Di\Exception\Unbound;
use Ray\Di\InjectorInterface;
use Ray\PsrCacheModule\Annotation\Shared;
use Symfony\Component\Cache\Adapter\AdapterInterface;
Expand Down Expand Up @@ -52,13 +55,34 @@ final public function __serialize(): array
* @param array{logger: RepositoryLoggerInterface, purger: PurgerInterface, uriTag: UriTagInterface, saver: ResourceStorageSaver, knownTagTtl: float, injector: InjectorInterface} $data
*/
final public function __unserialize(array $data): void
{
try {
$this->unserialize($data);
// @codeCoverageIgnoreStart
} catch (Error $e) {
throw new ResourceStorageUnserializeException($e->getMessage());
// @codeCoverageIgnoreEnd
}
}

/**
* @param array{logger: RepositoryLoggerInterface, purger: PurgerInterface, uriTag: UriTagInterface, saver: ResourceStorageSaver, knownTagTtl: float, injector: InjectorInterface} $data
*/
private function unserialize(array $data): void
{
$this->logger = $data['logger'];
$this->purger = $data['purger'];
$this->uriTag = $data['uriTag'];
$this->saver = $data['saver'];
$pool = $data['injector']->getInstance(CacheItemPoolInterface::class, Shared::class);
$maybeEtagPool = $data['injector']->getInstance(CacheItemPoolInterface::class, EtagPool::class);
try {
$maybeEtagPool = $data['injector']->getInstance(CacheItemPoolInterface::class, EtagPool::class);
// @codeCoverageIgnoreStart
} catch (Unbound $e) {
$maybeEtagPool = null;
// @codeCoverageIgnoreEnd
}

assert($pool instanceof AdapterInterface);
/** @psalm-suppress all */
$etagPool = $maybeEtagPool instanceof AdapterInterface ? $maybeEtagPool : $pool;
Expand Down
4 changes: 3 additions & 1 deletion src/ResourceStorageInterface.php
Expand Up @@ -52,8 +52,10 @@ public function getDonut(AbstractUri $uri): ?ResourceDonut;

/**
* Save donut-cacheable page
*
* @param list<string> $headerKeys
*/
public function saveDonut(AbstractUri $uri, ResourceDonut $donut, ?int $sMaxAge): void;
public function saveDonut(AbstractUri $uri, ResourceDonut $donut, ?int $sMaxAge, array $headerKeys): void;

/**
* Save donut-cache state
Expand Down
2 changes: 1 addition & 1 deletion src/SurrogateKeys.php
Expand Up @@ -18,7 +18,7 @@

final class SurrogateKeys
{
/** @var array<string> */
/** @var list<string> */
private array $surrogateKeys;
private UriTagInterface $uriTag;

Expand Down
2 changes: 1 addition & 1 deletion tests/ResourceStorageTest.php
Expand Up @@ -30,7 +30,7 @@ protected function setUp(): void
public function testSaveGetStatic(): void
{
$donut = ResourceDonut::create($this->ro, new DonutRenderer(), new SurrogateKeys(new Uri('app://self/')), null, false);
$this->storage->saveDonut($this->ro->uri, $donut, null);
$this->storage->saveDonut($this->ro->uri, $donut, null, []);
$donut = $this->storage->getDonut($this->ro->uri);
$this->assertInstanceOf(ResourceDonut::class, $donut);
}
Expand Down

0 comments on commit 434f1d3

Please sign in to comment.