-
Notifications
You must be signed in to change notification settings - Fork 690
[6.x] ElementCaches #18647
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
Merged
Merged
[6.x] ElementCaches #18647
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e23f625
Extract ElementCaches service from Elements service
riasvdv 1e6f784
Changelog
riasvdv 9cc87f0
Port events
riasvdv c73b808
Make ElementCaches a singleton
riasvdv 5146905
Improve test coverage
riasvdv 8dad2e4
Fix GC test
riasvdv 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,163 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace CraftCms\Cms\Element; | ||
|
|
||
| use craft\base\ElementInterface; | ||
| use craft\base\NestedElementInterface; | ||
| use CraftCms\Cms\Element\Events\InvalidateElementCaches; | ||
| use CraftCms\Cms\View\CacheCollectors\DependencyCollector; | ||
| use CraftCms\Cms\View\Data\TemplateCacheContext; | ||
| use CraftCms\DependencyAwareCache\Dependency\TagDependency; | ||
| use DateTime; | ||
| use Illuminate\Container\Attributes\Singleton; | ||
| use Throwable; | ||
| use yii\base\InvalidConfigException; | ||
|
|
||
| #[Singleton] | ||
| readonly class ElementCaches | ||
| { | ||
| public function isCollectingCacheInfo(): bool | ||
| { | ||
| return $this->dependencyCollector()->isCollecting(); | ||
| } | ||
|
|
||
| public function startCollectingCacheInfo(): void | ||
| { | ||
| $this->dependencyCollector()->begin(new TemplateCacheContext( | ||
| cacheKey: '', | ||
| global: false, | ||
| resources: false, | ||
| )); | ||
| } | ||
|
|
||
| /** @param list<string> $tags */ | ||
| public function collectCacheTags(array $tags): void | ||
| { | ||
| $this->dependencyCollector()->collectTags($tags); | ||
| } | ||
|
|
||
| public function setCacheExpiryDate(DateTime $expiryDate): void | ||
| { | ||
| $this->dependencyCollector()->setExpiryDate($expiryDate); | ||
| } | ||
|
|
||
| public function collectCacheInfoForElement(ElementInterface $element): void | ||
| { | ||
| $this->dependencyCollector()->collectElement($element); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{TagDependency|null, int|null} | ||
| */ | ||
| public function stopCollectingCacheInfo(): array | ||
| { | ||
| return $this->dependencyCollector()->stop(); | ||
|
riasvdv marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** @return list<string> */ | ||
| public function invalidateAll(): array | ||
| { | ||
| $tags = $this->invalidateTags(['element']); | ||
|
|
||
| event(new InvalidateElementCaches($tags)); | ||
|
|
||
| return $tags; | ||
| } | ||
|
|
||
| /** | ||
| * @param class-string<ElementInterface> $elementType | ||
| * @return list<string> | ||
| */ | ||
| public function invalidateForElementType(string $elementType): array | ||
| { | ||
| $tags = $this->invalidateTags(["element::$elementType"]); | ||
|
|
||
| event(new InvalidateElementCaches($tags)); | ||
|
|
||
| return $tags; | ||
| } | ||
|
|
||
| /** | ||
| * @return list<string> | ||
| */ | ||
| public function invalidateForElement(ElementInterface $element): array | ||
| { | ||
| $tags = $this->invalidateTags($this->tagsForElement($element)); | ||
|
|
||
| event(new InvalidateElementCaches($tags, $element)); | ||
|
|
||
| return $tags; | ||
| } | ||
|
|
||
| /** @return list<string> */ | ||
| private function invalidateTags(array $tags): array | ||
| { | ||
| TagDependency::invalidate($tags); | ||
|
|
||
| return $tags; | ||
| } | ||
|
|
||
| /** @return list<string> */ | ||
| private function tagsForElement(ElementInterface $element): array | ||
| { | ||
| $elementClass = $element::class; | ||
| $tags = [ | ||
| "element::{$elementClass}::*", | ||
| "element::{$element->id}", | ||
| ]; | ||
|
|
||
| $rootElement = $element; | ||
|
|
||
| if ($element instanceof NestedElementInterface) { | ||
| try { | ||
| $owner = $element->getOwner(); | ||
| } catch (InvalidConfigException) { | ||
| $owner = null; | ||
| } | ||
|
|
||
| if ($owner) { | ||
| $tags[] = "element::{$owner->id}"; | ||
|
|
||
| try { | ||
| $rootElement = $owner->getRootOwner(); | ||
| } catch (Throwable) { | ||
| $rootElement = $owner; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ($rootElement->getIsDraft()) { | ||
| $tags[] = "element::{$elementClass}::drafts"; | ||
|
|
||
| return $tags; | ||
| } | ||
|
|
||
| if ($rootElement->getIsRevision()) { | ||
| $tags[] = "element::{$elementClass}::revisions"; | ||
|
|
||
| return $tags; | ||
| } | ||
|
|
||
| foreach ($element->getCacheTags() as $tag) { | ||
| if (! str_starts_with($tag, 'element::')) { | ||
| $tag = "element::{$elementClass}::{$tag}"; | ||
| } | ||
|
|
||
| $tags[] = $tag; | ||
| } | ||
|
|
||
| return $tags; | ||
| } | ||
|
|
||
| /** | ||
| * We specifically want to resolve the DependencyCollector | ||
| * from the container, as it is a scoped service, and | ||
| * we don't want it locked inside this singleton. | ||
| */ | ||
| private function dependencyCollector(): DependencyCollector | ||
| { | ||
| return app(DependencyCollector::class); | ||
| } | ||
| } | ||
This file contains hidden or 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,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace CraftCms\Cms\Element\Events; | ||
|
|
||
| use craft\base\ElementInterface; | ||
| use CraftCms\Cms\Element\ElementCaches; | ||
|
|
||
|
riasvdv marked this conversation as resolved.
|
||
| class InvalidateElementCaches | ||
| { | ||
| public function __construct( | ||
| /** | ||
| * @var string[] An array of TagDependency tag names that are being invalidated | ||
| */ | ||
| public array $tags, | ||
|
|
||
| /** | ||
| * @var ElementInterface|null The element whose caches are being invalidated, if this was triggered from {@see ElementCaches::invalidateForElement()}. | ||
| */ | ||
| public ?ElementInterface $element = null, | ||
| ) {} | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.