Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"phpstan/phpstan": "^2.1.11",
"phpstan/phpstan-phpunit": "^2.0",
"phpunit/phpunit": "^11.5.15",
"symfony/cache": "^6.4.0 || ^7.0.0 || ^8.0.0",
"symfony/messenger": "^5.4.31 || ^6.4.0 || ^7.0.1 || ^8.0.0",
"symfony/var-dumper": "^5.4.29 || ^6.4.0 || ^7.0.0 || ^8.0.0",
"wnx/commonmark-markdown-renderer": "^1.5.0"
Expand Down
181 changes: 180 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Patchlevel\EventSourcing\Metadata\AggregateRoot;

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

final class Psr16AggregateRootMetadataFactory implements AggregateRootMetadataFactory
Expand All @@ -24,16 +25,15 @@ public function __construct(
*/
public function metadata(string $aggregate): AggregateRootMetadata
{
/** @var ?AggregateRootMetadata<T> $metadata */
$metadata = $this->cache->get($aggregate);
$metadata = $this->cache->get(CacheKey::forAggregateRoot($aggregate));

if ($metadata !== null) {
if ($metadata instanceof AggregateRootMetadata && $metadata->className === $aggregate) {
return $metadata;
}

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

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

return $metadata;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Patchlevel\EventSourcing\Metadata\AggregateRoot;

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

final class Psr6AggregateRootMetadataFactory implements AggregateRootMetadataFactory
Expand All @@ -24,13 +25,14 @@ public function __construct(
*/
public function metadata(string $aggregate): AggregateRootMetadata
{
$item = $this->cache->getItem($aggregate);
$item = $this->cache->getItem(CacheKey::forAggregateRoot($aggregate));

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

return $data;
if ($data instanceof AggregateRootMetadata && $data->className === $aggregate) {
return $data;
}
}

$metadata = $this->aggregateRootMetadataFactory->metadata($aggregate);
Expand Down
35 changes: 35 additions & 0 deletions src/Metadata/CacheKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Metadata;

use function hash;

/**
* PSR-6 and PSR-16 reserve the characters {}()/\@: and only guarantee keys up to 64 characters,
* so class names are hashed instead of being used as they are.
* The prefix keeps the metadata types apart if they share the same cache.
*
* @internal
*/
final class CacheKey
{
/** @param class-string $class */
public static function forAggregateRoot(string $class): string
{
return 'aggregate_root_metadata_' . hash('xxh128', $class);
}

/** @param class-string $class */
public static function forEvent(string $class): string
{
return 'event_metadata_' . hash('xxh128', $class);
}

/** @param class-string $class */
public static function forSubscriber(string $class): string
{
return 'subscriber_metadata_' . hash('xxh128', $class);
}
}
8 changes: 4 additions & 4 deletions src/Metadata/Event/Psr16EventMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Patchlevel\EventSourcing\Metadata\Event;

use Patchlevel\EventSourcing\Metadata\CacheKey;
use Psr\SimpleCache\CacheInterface;

final class Psr16EventMetadataFactory implements EventMetadataFactory
Expand All @@ -17,16 +18,15 @@ public function __construct(
/** @param class-string $event */
public function metadata(string $event): EventMetadata
{
/** @var ?EventMetadata $metadata */
$metadata = $this->cache->get($event);
$metadata = $this->cache->get(CacheKey::forEvent($event));

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

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

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

return $metadata;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Metadata/Event/Psr6EventMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace Patchlevel\EventSourcing\Metadata\Event;

use Patchlevel\EventSourcing\Metadata\CacheKey;
use Psr\Cache\CacheItemPoolInterface;

use function assert;

final class Psr6EventMetadataFactory implements EventMetadataFactory
{
public function __construct(
Expand All @@ -19,13 +18,14 @@ public function __construct(
/** @param class-string $event */
public function metadata(string $event): EventMetadata
{
$item = $this->cache->getItem($event);
$item = $this->cache->getItem(CacheKey::forEvent($event));

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

return $data;
if ($data instanceof EventMetadata) {
return $data;
}
}

$metadata = $this->eventMetadataFactory->metadata($event);
Expand Down
8 changes: 4 additions & 4 deletions src/Metadata/Subscriber/Psr16SubscriberMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Patchlevel\EventSourcing\Metadata\Subscriber;

use Patchlevel\EventSourcing\Metadata\CacheKey;
use Psr\SimpleCache\CacheInterface;

final class Psr16SubscriberMetadataFactory implements SubscriberMetadataFactory
Expand All @@ -17,16 +18,15 @@ public function __construct(
/** @param class-string $subscriber */
public function metadata(string $subscriber): SubscriberMetadata
{
/** @var ?SubscriberMetadata $metadata */
$metadata = $this->cache->get($subscriber);
$metadata = $this->cache->get(CacheKey::forSubscriber($subscriber));

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

$metadata = $this->subscriberMetadataFactory->metadata($subscriber);

$this->cache->set($subscriber, $metadata);
$this->cache->set(CacheKey::forSubscriber($subscriber), $metadata);

return $metadata;
}
Expand Down
Loading
Loading