Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add metadata cache #420

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/Metadata/AggregateRoot/Psr16AggregateRootMetadataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Metadata\AggregateRoot;

use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use Psr\SimpleCache\CacheInterface;

final class Psr16AggregateRootMetadataFactory implements AggregateRootMetadataFactory
{
public function __construct(
private readonly AggregateRootMetadataFactory $aggregateRootMetadataFactory,
private readonly CacheInterface $cache,
) {
}

/**
* @param class-string<T> $aggregate
*
* @return AggregateRootMetadata<T>
*
* @template T of AggregateRoot
*/
public function metadata(string $aggregate): AggregateRootMetadata
{
/** @var ?AggregateRootMetadata<T> $metadata */
$metadata = $this->cache->get($aggregate);

if ($metadata !== null) {
return $metadata;
}

$metadata = $this->aggregateRootMetadataFactory->metadata($aggregate);

$this->cache->set($aggregate, $metadata);

return $metadata;
}
}
33 changes: 33 additions & 0 deletions src/Metadata/AggregateRoot/Psr16AggregateRootRegistryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Metadata\AggregateRoot;

use Psr\SimpleCache\CacheInterface;

final class Psr16AggregateRootRegistryFactory implements AggregateRootRegistryFactory
{
public function __construct(
private readonly AggregateRootRegistryFactory $aggregateRootRegistryFactory,
private readonly CacheInterface $cache,
) {
}

/** @param list<string> $paths */
public function create(array $paths): AggregateRootRegistry
{
/** @var ?AggregateRootRegistry $registry */
$registry = $this->cache->get('aggregate_roots');
DavidBadura marked this conversation as resolved.
Show resolved Hide resolved

if ($registry !== null) {
return $registry;
}

$registry = $this->aggregateRootRegistryFactory->create($paths);

$this->cache->set('aggregate_roots', $registry);

return $registry;
}
}
43 changes: 43 additions & 0 deletions src/Metadata/AggregateRoot/Psr6AggregateRootMetadataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Metadata\AggregateRoot;

use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use Psr\Cache\CacheItemPoolInterface;

final class Psr6AggregateRootMetadataFactory implements AggregateRootMetadataFactory
{
public function __construct(
private readonly AggregateRootMetadataFactory $aggregateRootMetadataFactory,
private readonly CacheItemPoolInterface $cache,
) {
}

/**
* @param class-string<T> $aggregate
*
* @return AggregateRootMetadata<T>
*
* @template T of AggregateRoot
*/
public function metadata(string $aggregate): AggregateRootMetadata
{
$item = $this->cache->getItem($aggregate);

if ($item->isHit()) {
/** @var AggregateRootMetadata<T> $data */
$data = $item->get();

return $data;
}

$metadata = $this->aggregateRootMetadataFactory->metadata($aggregate);

$item->set($metadata);
$this->cache->save($item);

return $metadata;
}
}
38 changes: 38 additions & 0 deletions src/Metadata/AggregateRoot/Psr6AggregateRootRegistryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Metadata\AggregateRoot;

use Psr\Cache\CacheItemPoolInterface;

use function assert;

final class Psr6AggregateRootRegistryFactory implements AggregateRootRegistryFactory
{
public function __construct(
private readonly AggregateRootRegistryFactory $aggregateRootRegistryFactory,
private readonly CacheItemPoolInterface $cache,
) {
}

/** @param list<string> $paths */
public function create(array $paths): AggregateRootRegistry
{
$item = $this->cache->getItem('aggregate_roots');
DavidBadura marked this conversation as resolved.
Show resolved Hide resolved

if ($item->isHit()) {
$data = $item->get();
assert($data instanceof AggregateRootRegistry);

return $data;
}

$registry = $this->aggregateRootRegistryFactory->create($paths);

$item->set($registry);
$this->cache->save($item);

return $registry;
}
}
33 changes: 33 additions & 0 deletions src/Metadata/Event/Psr16EventMetadataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Metadata\Event;

use Psr\SimpleCache\CacheInterface;

final class Psr16EventMetadataFactory implements EventMetadataFactory
{
public function __construct(
private readonly EventMetadataFactory $eventMetadataFactory,
private readonly CacheInterface $cache,
) {
}

/** @param class-string $event */
public function metadata(string $event): EventMetadata
{
/** @var ?EventMetadata $metadata */
$metadata = $this->cache->get($event);

if ($metadata !== null) {
return $metadata;
}

$metadata = $this->eventMetadataFactory->metadata($event);

$this->cache->set($event, $metadata);

return $metadata;
}
}
33 changes: 33 additions & 0 deletions src/Metadata/Event/Psr16EventRegistryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Metadata\Event;

use Psr\SimpleCache\CacheInterface;

final class Psr16EventRegistryFactory implements EventRegistryFactory
{
public function __construct(
private readonly EventRegistryFactory $eventRegistryFactory,
private readonly CacheInterface $cache,
) {
}

/** @param list<string> $paths */
public function create(array $paths): EventRegistry
{
/** @var ?EventRegistry $registry */
$registry = $this->cache->get('events');
DavidBadura marked this conversation as resolved.
Show resolved Hide resolved

if ($registry !== null) {
return $registry;
}

$registry = $this->eventRegistryFactory->create($paths);

$this->cache->set('events', $registry);

return $registry;
}
}
38 changes: 38 additions & 0 deletions src/Metadata/Event/Psr6EventMetadataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Metadata\Event;

use Psr\Cache\CacheItemPoolInterface;

use function assert;

final class Psr6EventMetadataFactory implements EventMetadataFactory
{
public function __construct(
private readonly EventMetadataFactory $eventMetadataFactory,
private readonly CacheItemPoolInterface $cache,
) {
}

/** @param class-string $event */
public function metadata(string $event): EventMetadata
{
$item = $this->cache->getItem($event);

if ($item->isHit()) {
$data = $item->get();
assert($data instanceof EventMetadata);

return $data;
}

$metadata = $this->eventMetadataFactory->metadata($event);

$item->set($metadata);
$this->cache->save($item);

return $metadata;
}
}
38 changes: 38 additions & 0 deletions src/Metadata/Event/Psr6EventRegistryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Metadata\Event;

use Psr\Cache\CacheItemPoolInterface;

use function assert;

final class Psr6EventRegistryFactory implements EventRegistryFactory
{
public function __construct(
private readonly EventRegistryFactory $eventRegistryFactory,
private readonly CacheItemPoolInterface $cache,
) {
}

/** @param list<string> $paths */
public function create(array $paths): EventRegistry
{
$item = $this->cache->getItem('events');
DavidBadura marked this conversation as resolved.
Show resolved Hide resolved

if ($item->isHit()) {
$data = $item->get();
assert($data instanceof EventRegistry);

return $data;
}

$registry = $this->eventRegistryFactory->create($paths);

$item->set($registry);
$this->cache->save($item);

return $registry;
}
}
34 changes: 34 additions & 0 deletions src/Metadata/Projector/Psr16ProjectorMetadataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Metadata\Projector;

use Patchlevel\EventSourcing\Projection\Projector\Projector;
use Psr\SimpleCache\CacheInterface;

final class Psr16ProjectorMetadataFactory implements ProjectorMetadataFactory
{
public function __construct(
private readonly ProjectorMetadataFactory $projectorMetadataFactory,
private readonly CacheInterface $cache,
) {
}

/** @param class-string<Projector> $projector */
public function metadata(string $projector): ProjectorMetadata
{
/** @var ?ProjectorMetadata $metadata */
$metadata = $this->cache->get($projector);

if ($metadata !== null) {
return $metadata;
}

$metadata = $this->projectorMetadataFactory->metadata($projector);

$this->cache->set($projector, $metadata);

return $metadata;
}
}
39 changes: 39 additions & 0 deletions src/Metadata/Projector/Psr6ProjectorMetadataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Metadata\Projector;

use Patchlevel\EventSourcing\Projection\Projector\Projector;
use Psr\Cache\CacheItemPoolInterface;

use function assert;

final class Psr6ProjectorMetadataFactory implements ProjectorMetadataFactory
{
public function __construct(
private readonly ProjectorMetadataFactory $projectorMetadataFactory,
private readonly CacheItemPoolInterface $cache,
) {
}

/** @param class-string<Projector> $projector */
public function metadata(string $projector): ProjectorMetadata
{
$item = $this->cache->getItem($projector);

if ($item->isHit()) {
$data = $item->get();
assert($data instanceof ProjectorMetadata);

return $data;
}

$metadata = $this->projectorMetadataFactory->metadata($projector);

$item->set($metadata);
$this->cache->save($item);

return $metadata;
}
}
2 changes: 1 addition & 1 deletion src/Snapshot/Adapter/Psr16SnapshotAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
final class Psr16SnapshotAdapter implements SnapshotAdapter
{
public function __construct(
private CacheInterface $cache,
private readonly CacheInterface $cache,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Snapshot/Adapter/Psr6SnapshotAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
final class Psr6SnapshotAdapter implements SnapshotAdapter
{
public function __construct(
private CacheItemPoolInterface $cache,
private readonly CacheItemPoolInterface $cache,
) {
}

Expand Down