-
-
Notifications
You must be signed in to change notification settings - Fork 862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Instruct Doctrine Orm Bridge to use result cache or second level cache #2789
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
276e8f9
Added Cache QueryExpander
st-it ee79cbf
Implemented QueryExpander
st-it b3cad08
Implemented QueryExpander into tests
st-it bdc3d4a
Added test for QueryExpander
st-it ad5c74c
Added attribute definitions to ApiResource
st-it aa67d59
Merge branch 'second-level-cache'
st-it 212ba4b
Wrapped all cache config options into their own namespace
st-it fbe6e7d
Added doctrineCache attribute removed wrapped attributes
st-it c68cf64
Fixed code style for test
st-it cfba6f8
Fixed namespace
st-it 12460c1
Support the legacy scenario for PaginationExtension
st-it 6e85f6d
Fixed code style
st-it 1881c72
Fixed code style
st-it 8491380
Fixed argument order
st-it ed00974
Fixed regression
st-it File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\Core\Bridge\Doctrine\Orm\Cache; | ||
|
||
use ApiPlatform\Core\Exception\ResourceClassNotFoundException; | ||
use ApiPlatform\Core\Exception\RuntimeException; | ||
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; | ||
use Doctrine\ORM\AbstractQuery; | ||
|
||
/** | ||
* Class QueryExpander. | ||
* | ||
* Expands Query settings with Second Level Cache and Result Cache settings from attributes. | ||
* | ||
* @author st-it <[email protected]> | ||
*/ | ||
class QueryExpander | ||
{ | ||
public const DOCTRINE_CACHE_CONFIG_ATTR = 'doctrine_cache'; | ||
public const CACHEABLE_ATTR = 'cacheable'; | ||
public const CACHE_HINT_ATTR = 'cache_hint'; | ||
public const CACHE_MODE_ATTR = 'cache_mode'; | ||
public const USE_RESULT_CACHE_ATTR = 'use_result_cache'; | ||
|
||
private $resourceMetadataFactory; | ||
|
||
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory) | ||
{ | ||
$this->resourceMetadataFactory = $resourceMetadataFactory; | ||
} | ||
|
||
/** | ||
* @throws ResourceClassNotFoundException | ||
*/ | ||
public function expand(string $resourceClass, AbstractQuery $query) | ||
{ | ||
$cacheConfig = $this->getDoctrineCacheConfig($resourceClass); | ||
if (null !== $cacheConfig) { | ||
$this->processCacheable($cacheConfig, $query); | ||
$this->processCacheHint($cacheConfig, $query); | ||
$this->processCacheMode($cacheConfig, $query); | ||
$this->processResultCache($cacheConfig, $query); | ||
} | ||
} | ||
|
||
/** | ||
* @throws ResourceClassNotFoundException | ||
*/ | ||
private function getDoctrineCacheConfig(string $resourceClass): ?array | ||
{ | ||
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); | ||
$config = $resourceMetadata->getAttribute(self::DOCTRINE_CACHE_CONFIG_ATTR, null); | ||
|
||
return \is_array($config) ? $config : null; | ||
} | ||
|
||
private function processCacheable(array $cacheConfig, AbstractQuery $query) | ||
{ | ||
if (false !== ($cacheConfig[self::CACHEABLE_ATTR] ?? false)) { | ||
$query->setCacheable(true); | ||
} | ||
} | ||
|
||
/** | ||
* @throws RuntimeException | ||
*/ | ||
private function processCacheHint(array $cacheConfig, AbstractQuery $query) | ||
{ | ||
if (null !== $cacheHint = $cacheConfig[self::CACHE_HINT_ATTR] ?? null) { | ||
if (!\is_array($cacheHint)) { | ||
throw new RuntimeException(sprintf('Attribute value %s should be an array', self::CACHE_HINT_ATTR)); | ||
} | ||
foreach ($cacheHint as $name => $value) { | ||
$query->setHint($name, $value); | ||
} | ||
} | ||
} | ||
|
||
private function processCacheMode(array $cacheConfig, AbstractQuery $query) | ||
{ | ||
if (null !== $cacheMode = $cacheConfig[self::CACHE_MODE_ATTR] ?? null) { | ||
$query->setCacheMode($cacheMode); | ||
} | ||
} | ||
|
||
/** | ||
* @throws RuntimeException | ||
*/ | ||
private function processResultCache(array $cacheConfig, AbstractQuery $query) | ||
{ | ||
if (null !== $useResultCache = $cacheConfig[self::USE_RESULT_CACHE_ATTR] ?? null) { | ||
if (!\is_array($useResultCache)) { | ||
throw new RuntimeException(sprintf('Attribute value %s should be an array', self::USE_RESULT_CACHE_ATTR)); | ||
} | ||
if (empty($useResultCache) || 3 < \count($useResultCache)) { | ||
throw new RuntimeException(sprintf('Attribute %s should at least contain one item for use. Other options are lifetime and and result cache id', self::USE_RESULT_CACHE_ATTR)); | ||
} | ||
$query->useResultCache(...$useResultCache); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class should be an extension that alters the query probably implementing
QueryResult{Collection|Item}ExtensionInterface
.