Problem/Motivation
Avoid writing invalid data to the fast backend.
Only relevant if/when someone calls getMultiple() with invalid == TRUE.
Marking issue NOVICE ONLY for creating a patch out of the below code.
Steps to reproduce
- Call getMultiple() with $allow_invalid = TRUE.
- See that fastBackend is happily setting invalid data, which will never be useful
Proposed resolution
<code>
diff --git a/web/core/lib/Drupal/Core/Cache/ChainedFastBackend.php b/web/core/lib/Drupal/Core/Cache/ChainedFastBackend.php
--- a/web/core/lib/Drupal/Core/Cache/ChainedFastBackend.php
+++ b/web/core/lib/Drupal/Core/Cache/ChainedFastBackend.php
@@ -166,7 +166,10 @@ public function getMultiple(&$cids, $allow_invalid = FALSE) {
$cache[$item->cid] = $item;
// Don't write the cache tags to the fast backend as any cache tag
// invalidation results in an invalidation of the whole fast backend.
- $this->fastBackend->set($item->cid, $item->data, $item->expire);
+ if (!$allow_invalid || $item->valid) {
+ $this->fastBackend->set($item->cid, $item->data, $item->expire);
+ }
}
}
Remaining tasks
Review
User interface changes
N/A
API changes
N/A
Data model changes
N/A
Release notes snippet
N/A
| Comment | File | Size | Author |
|---|
Issue fork drupal-3395212
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3395212-ensure-invalid-items
changes, plain diff MR !9730
Comments
Comment #2
deborahblessy commentedIam working on this.
Comment #3
deborahblessy commentedHi @Fabianx,
I have created and attached the patch. Could you please verify this?
Comment #4
deborahblessy commentedComment #5
wim leersThank you, @deborahblessy!
Next step: test coverage.
Comment #6
smustgrave commentedFor #5
Comment #7
wim leersCatching up on long-forgotten tabs … from last year’s European DrupalCon just before this year’s starts 😄
I think Fabian is best suited to determine what is next here.
Comment #8
smustgrave commentedBrought this up in #contribute in slack https://drupal.slack.com/archives/C1BMUQ9U6/p1727876410850979
@fabianx agreed on the approach but mentioned still needs tests.
Comment #11
nicxvan commentedMoved to MR so tests can run and be more easily written.
Comment #13
murilohp commentedHey, just created a new test to validate the proposed solution, moving this to NR and removing the tag for now, thanks!
Comment #14
smustgrave commentedShows test coverage.
Not sure if this needs any kind of release note (assuming not)
Looking at the change to core/lib/Drupal/Core/Cache/ChainedFastBackend.php and don't see anything glaringly wrong. Think adding the condition makes sense.
believe this one is good.
Comment #15
kristiaanvandeneyndeGave the IS some thought and at first felt like this was wrong:
After all, if you're getting the data while note caring about its validity, you may as well store that in memory for future gets which do not care about validity. However, then I realized that a cache get which allows invalid could the poison the memory cache for a subsequent cache get which does not allow invalid. And that would be really bad.
I still have the eerie feeling that there are edge cases we're not seeing here, but for now I'd say it makes more sense to commit this change rather than keep the old behavior.
So RTBC +1.
Comment #20
catchI think this is worth skipping just from a cache efficiency point of view if nothing else - allow_invalid is most likely to be used in the case that stale content is served and then a cache refresh job is queued (or done end of request or similar), so there'll be a new cache write pretty fast after this gets set anyway. Cache poisoning seems theoretical because the fast backend should handle invalid items correctly, but it doesn't hurt.
Committed/pushed to 11.x/11.1.x/10.5.x/10.4.x, thanks!