Skip to content

Commit

Permalink
add metadata cache
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Oct 8, 2023
1 parent 5ce6710 commit 55e28a7
Show file tree
Hide file tree
Showing 12 changed files with 373 additions and 2 deletions.
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 $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');

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

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

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

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

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Metadata\AggregateRoot;

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

use function assert;

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()) {
$data = $item->get();
assert($data instanceof AggregateRootMetadata);

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');

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');

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');

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

0 comments on commit 55e28a7

Please sign in to comment.