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
66 changes: 66 additions & 0 deletions src/Metadata/Operation/Factory/CacheOperationMetadataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Metadata\Operation\Factory;

use ApiPlatform\Metadata\Operation;
use Psr\Cache\CacheException;
use Psr\Cache\CacheItemPoolInterface;

/**
* Caches operation metadata.
*
* @author Antoine Bluchet <soyuka@gmail.com>
*/
final class CacheOperationMetadataFactory implements OperationMetadataFactoryInterface
{
public const CACHE_KEY_PREFIX = 'operation_metadata_';
private array $localCache = [];

public function __construct(private readonly CacheItemPoolInterface $cacheItemPool, private readonly OperationMetadataFactoryInterface $decorated)
{
}

/**
* {@inheritdoc}
*/
public function create(string $uriTemplate, array $context = []): ?Operation
{
$cacheKey = self::CACHE_KEY_PREFIX.hash('xxh3', $uriTemplate);
if (\array_key_exists($cacheKey, $this->localCache)) {
return $this->localCache[$cacheKey];
}

try {
$cacheItem = $this->cacheItemPool->getItem($cacheKey);
} catch (CacheException) {
$operation = $this->decorated->create($uriTemplate, $context);
$this->localCache[$cacheKey] = $operation;

return $operation;
}

if ($cacheItem->isHit()) {
$this->localCache[$cacheKey] = $cacheItem->get();

return $this->localCache[$cacheKey];
}

$operation = $this->decorated->create($uriTemplate, $context);
$this->localCache[$cacheKey] = $operation;
$cacheItem->set($this->localCache[$cacheKey]);
$this->cacheItemPool->save($cacheItem);

return $operation;
}
}
8 changes: 7 additions & 1 deletion src/Metadata/Operation/Factory/OperationMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@

final class OperationMetadataFactory implements OperationMetadataFactoryInterface
{
private array $localCache = [];

public function __construct(private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory)
{
}

public function create(string $uriTemplate, array $context = []): ?Operation
{
if (isset($this->localCache[$uriTemplate])) {
return $this->localCache[$uriTemplate];
}

foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
foreach ($this->resourceMetadataCollectionFactory->create($resourceClass) as $resource) {
foreach ($resource->getOperations() as $operation) {
if ($operation->getUriTemplate() === $uriTemplate || $operation->getName() === $uriTemplate) {
return $operation;
return $this->localCache[$uriTemplate] = $operation;
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Bundle/Resources/config/metadata/operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@
]);

$services->alias('ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface', 'api_platform.metadata.operation.metadata_factory');

$services->set('api_platform.metadata.operation.metadata_factory.cached', 'ApiPlatform\Metadata\Operation\Factory\CacheOperationMetadataFactory')
->decorate('api_platform.metadata.operation.metadata_factory', null, -10)
->args([
service('api_platform.cache.metadata.operation'),
service('api_platform.metadata.operation.metadata_factory.cached.inner'),
]);

$services->set('api_platform.cache.metadata.operation')
->parent('cache.system')
->tag('cache.pool');
};
Loading