Skip to content

Commit

Permalink
Re-use previously created event store instances
Browse files Browse the repository at this point in the history
  • Loading branch information
bwaidelich authored and mhsdesign committed Oct 13, 2024
1 parent fcdbbf5 commit f32c825
Showing 1 changed file with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,39 @@
use Doctrine\ORM\EntityManagerInterface;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\EventStore\DoctrineAdapter\DoctrineEventStore;
use Neos\EventStore\EventStoreInterface;
use Psr\Clock\ClockInterface;

class DoctrineEventStoreFactory implements EventStoreFactoryInterface
{
/**
* @var array<string, DoctrineEventStore> Runtime cache for created event store instances to prevent too many connections
*/
private static array $instances = [];

public function __construct(
private readonly EntityManagerInterface $entityManager,
) {
}

/** @param array<string, mixed> $options */
public function build(ContentRepositoryId $contentRepositoryId, array $options, ClockInterface $clock): EventStoreInterface
public function build(ContentRepositoryId $contentRepositoryId, array $options, ClockInterface $clock): DoctrineEventStore
{
// We create a new connection instance in order to avoid nested transactions
$connection = DriverManager::getConnection($this->entityManager->getConnection()->getParams(), $this->entityManager->getConfiguration(), $this->entityManager->getEventManager());
return new DoctrineEventStore(
$connection,
self::databaseTableName($contentRepositoryId),
$clock
);
$dsn = $options['dsn'] ?? null;
$hash = md5($contentRepositoryId->value . '|' . $clock::class . '|' . $dsn);
if (!array_key_exists($hash, self::$instances)) {
if ($dsn !== null) {
$connection = DriverManager::getConnection(['url' => $dsn]);
} else {
// We create a new connection instance in order to avoid nested transactions
$connection = DriverManager::getConnection($this->entityManager->getConnection()->getParams(), $this->entityManager->getConfiguration(), $this->entityManager->getEventManager());
}
self::$instances[$hash] = new DoctrineEventStore(
$connection,
self::databaseTableName($contentRepositoryId),
$clock
);
}
return self::$instances[$hash];
}

public static function databaseTableName(ContentRepositoryId $contentRepositoryId): string
Expand Down

0 comments on commit f32c825

Please sign in to comment.