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 9, 2023
1 parent 7893c92 commit fe97e70
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 48 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"cspray/phinal": "^2.0",
"infection/infection": "^0.27.0",
"patchlevel/coding-standard": "^1.2.0",
"psr/cache": "^2.0.0|^3.0.0",
"psr/simple-cache": "^2.0.0|^3.0.0",
"phpbench/phpbench": "^1.2.6",
"phpspec/prophecy-phpunit": "^2.0.1",
"phpstan/phpstan": "^1.8.2",
Expand Down
147 changes: 99 additions & 48 deletions composer.lock

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

39 changes: 39 additions & 0 deletions src/Metadata/Psr16MetadataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Metadata;

use Psr\SimpleCache\CacheInterface;

final class Psr16MetadataFactory implements MetadataFactory
{
public function __construct(
private readonly MetadataFactory $metadataFactory,
private readonly CacheInterface $cache,
) {
}

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

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

$metadata = $this->metadataFactory->metadata($class);

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

return $metadata;
}
}
Loading

0 comments on commit fe97e70

Please sign in to comment.