Repository navigation
Scope cache activation #48
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
Open
jdreesen
wants to merge
11
commits into
main
Choose a base branch
from
scope-cache-activation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fca9f93
Scope cache activation
jdreesen 8f7817a
Add tests for document tagging in request scope, including parent and…
jdreesen 8ef02fa
Restore CacheActivator's previous behavior
jdreesen e4ccc29
Improve deprecation messages
jdreesen 4886e26
Enable cache scope in `CancelInvalidationTest` setup
jdreesen 154a718
Make `neusta_pimcore_http_cache.cache_scope` and `neusta_pimcore_http…
jdreesen 9fd3192
Add docs
jdreesen 884148d
Rename `CacheScope::isActive()` to `isEnabled()`
jdreesen 6764e22
Fix deprecation message inaccuracies in CacheActivator
jdreesen 77642a7
Decouple cache invalidation from the tagging scope
jdreesen 001d2db
Document that CacheActivator::activateCaching() can clear a disable()
jdreesen 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,15 @@ | ||
| # Changelog | ||
|
|
||
| ## Version 1.0.0 | ||
| ## Version 0.8.0 | ||
|
|
||
| - feature: Introduced `CacheScope` service for controlling when and whether cache tags are collected. Cache invalidation is active by default everywhere (including non-cacheable and admin-context requests) and only stops once `CacheScope::disable()` is called; tag collection instead requires the scope to be explicitly enabled for the current request | ||
| - feature: New `scope` configuration option (`controller` / `request`) to control when tag collection begins within a request | ||
| - feature: New `tag_fallback_document` configuration option to tag responses with fallback documents | ||
| - deprecation: `CacheActivator` is deprecated; use `CacheScope` instead | ||
| - deprecation: `CacheActivator::isCachingActive()` is deprecated; use `CacheScope::isInvalidating()` instead | ||
| - deprecation: `CacheActivator::activateCaching()` is deprecated; use `CacheScope::enable()` instead | ||
| - deprecation: `CacheActivator::deactivateCaching()` is deprecated; use `CacheScope::disable()` instead | ||
|
|
||
| ## Version 0.7.0 | ||
|
|
||
| - feature: Initial Release |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,70 @@ | ||
| ## Cache Scope | ||
|
|
||
| The cache scope controls whether and from which point in the request lifecycle cache-related behavior is enabled. | ||
|
|
||
| Tagging and invalidation are gated independently: | ||
|
|
||
| - **Tagging** only happens while the scope is **enabled**. Tagging only ever makes sense for a request whose response | ||
| can actually be cached, so the scope must be explicitly activated (see [Automatic activation](#automatic-activation)). | ||
| - **Invalidation** is active by default — everywhere, including non-cacheable requests such as an admin-UI save or a | ||
| custom `POST` endpoint — and only stops once `disable()` has been called. Invalidation does not depend on the scope | ||
| being enabled. | ||
|
|
||
| Calling `disable()` always stops both tagging and invalidation, regardless of the above. | ||
|
|
||
| ### Automatic activation | ||
|
|
||
| The bundle activates the scope automatically in two contexts: | ||
|
|
||
| - **HTTP requests**: The scope is activated for every cacheable request (`GET` or `HEAD`, non-admin context). | ||
| When it is activated depends on the [`scope` configuration option](#scope-option). | ||
| - **Console commands**: The scope is activated at the start of every console command. | ||
|
|
||
| This only affects tagging; invalidation runs regardless of whether the current request or command activates the scope. | ||
|
|
||
| ### Scope option | ||
|
|
||
| The `scope` option controls at which point in the request lifecycle the scope is activated for HTTP requests: | ||
|
|
||
| - `controller` (default): The scope is activated during the `kernel.controller` event. | ||
| Elements loaded earlier — e.g. in `kernel.request` event listeners — are **not** tagged. | ||
| - `request`: The scope is activated during the `kernel.request` event, i.e. from the very beginning of the request. | ||
| Use this if you need to tag elements that are loaded in `kernel.request` listeners. | ||
|
|
||
| ```yaml | ||
| neusta_pimcore_http_cache: | ||
| scope: controller # or "request" | ||
| ``` | ||
|
|
||
| ### Manual access | ||
|
|
||
| You can inject `CacheScope` directly to check or modify the scope state at runtime: | ||
|
|
||
| ```php | ||
| use Neusta\Pimcore\HttpCacheBundle\CacheScope; | ||
|
|
||
| final class MyService | ||
| { | ||
| public function __construct( | ||
| private readonly CacheScope $cacheScope, | ||
| ) { | ||
| } | ||
|
|
||
| public function doSomething(): void | ||
| { | ||
| if ($this->cacheScope->isEnabled()) { | ||
| // tagging is currently enabled | ||
| } | ||
|
|
||
| if ($this->cacheScope->isInvalidating()) { | ||
| // invalidation is currently active | ||
| } | ||
|
|
||
| $this->cacheScope->disable(); | ||
| // both tagging and invalidation are disabled from now on | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| To disable cache-related behavior for the current request or command, see | ||
| [Disabling caching behavior](9-disable-caching-behavior.md). |
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,61 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Neusta\Pimcore\HttpCacheBundle; | ||
|
|
||
| use Symfony\Contracts\Service\ResetInterface; | ||
|
|
||
| /** | ||
| * @final | ||
| */ | ||
| class CacheScope implements ResetInterface | ||
| { | ||
| private bool $enabled = false; | ||
| private bool $disabled = false; | ||
|
|
||
| /** | ||
| * Enables cache-related behavior until the scope is reset. | ||
| * | ||
| * Has no effect if {@see disable()} was already called. | ||
| */ | ||
| public function enable(): void | ||
| { | ||
| $this->enabled = true; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Disables cache-related behavior until the scope is reset. | ||
| * | ||
| * Survives {@see enable()} calls. | ||
| */ | ||
| public function disable(): void | ||
| { | ||
| $this->disabled = true; | ||
| } | ||
|
|
||
| /** | ||
| * Whether cache-related behavior is currently enabled. | ||
| */ | ||
| public function isEnabled(): bool | ||
| { | ||
| return $this->enabled && !$this->disabled; | ||
| } | ||
|
|
||
| /** | ||
| * Whether cache invalidation is currently active. | ||
| * | ||
| * Unlike {@see isEnabled()}, invalidation is active by default and only | ||
| * stops once {@see disable()} has been called; it does not require an | ||
| * explicit {@see enable()} call. | ||
| */ | ||
| public function isInvalidating(): bool | ||
| { | ||
| return !$this->disabled; | ||
| } | ||
|
|
||
| public function reset(): void | ||
| { | ||
| $this->enabled = false; | ||
| $this->disabled = false; | ||
| } | ||
| } | ||
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.