Move caching of block patterns to object cache, rather than transients.#5533
Move caching of block patterns to object cache, rather than transients.#5533joemcgill wants to merge 11 commits intoWordPress:trunkfrom
Conversation
This caches block patterns using `WP_Object_Cache` rather than transients. Caches are stored in the global group so sites with persistent caches can still get the performance benefit, while allowing this data to be cleared with a standard cache flush, while avoiding persistance issues for sites not using a persistent object cache.
There was a problem hiding this comment.
@joemcgill The approach looks reasonable to me, though I wonder about not using any specific cache group. Is there existing cache usage without a specific cache group in core? I'm not opposed of committing this without a specific cache group, but would want to make sure that's conforming with how core should use the object cache functions.
I realize you may be omitting this because neither the themes nor the theme_json groups are persistent cache groups, which we therefore shouldn't use here. We could still rely on a new group (which would thus be persistent as that is the default), maybe theme_files?
|
@felixarntz, my initial approach here is the lightest possible change—converting the transient functions to their As is, I think this is the minimum viable solution for 6.4. However, while working on this, I'd like to open consideration of two other changes: Cache GroupsI think keeping this in the global group for now is fine, and we can move it to a more appropriate group later, here are the alternatives I considered:
Optional architectural improvementsWP 6.4 adds three new public methods to the |
felixarntz
left a comment
There was a problem hiding this comment.
@joemcgill Thanks for clarifying. Fair points, I think we can go with the global group for now and iterate later as part of coming up with a more consistent approach in 6.5.
On a related note, regarding theme_json, I believe that group is non-persistent as well, so we wouldn't be able to use it anyway given we would like this data to be persistently cached for sites with an object cache.
It's also reassuring to see that all tests are still passing without any modifications 🎉
|
@joemcgill Regarding the additional architectural considerations:
Generally, I agree this would be a better solution. It's late in the cycle for this kind of change, but if we can make it happen with minimal code changes, let's do it. So we would remove |
I agree that it’s late. The only reason I feel it’s worth doing now is because once those methods ship, they need to be maintained, so now seems better. I’ll update this PR accordingly. |
This moves `_wp_get_block_patterns` to a new public method of the `WP_Theme` class called `get_block_patterns` and makes all of the related caching methods private to avoid future compatibility issues.
| return $this->block_template_folders; | ||
| } | ||
|
|
||
| /** |
There was a problem hiding this comment.
Reviewer note:
These methods are just moved from above, so they are organized with other block-related methods. The only changes are that the set_pattern_cache and get_pattern_cache are now private, while the delete_pattern_cache remains public for cache clearing purposes.
| @@ -1,35 +1,55 @@ | |||
| <?php | |||
There was a problem hiding this comment.
This file name needs to be updated, but let's agree on the method name first.
There was a problem hiding this comment.
See my other comment, maybe tests/phpunit/tests/theme/wpThemeGetBlockPatterns.php?
felixarntz
left a comment
There was a problem hiding this comment.
@joemcgill This looks great to me and pretty much good to go. A few last notes regarding your existing comments on naming.
| @@ -1,35 +1,55 @@ | |||
| <?php | |||
There was a problem hiding this comment.
See my other comment, maybe tests/phpunit/tests/theme/wpThemeGetBlockPatterns.php?
mukeshpanchal27
left a comment
There was a problem hiding this comment.
Thank you, @joemcgill, for the PR. I have some minor suggestions.
We should rename the file from wpGetBlockPatterns.php to getBlockPatterns.php since we didn't use the wp_ prefix.
|
Thanks @mukeshpanchal27! I've applied your message suggestions in c35134e. I renamed and moved the test file, per @felixarntz's suggestion in 2ff2d69. |
|
@joemcgill Not sure why, and you probably already saw it, but it looks like unit tests are currently failing. I'm not sure what you could have changed that led to this, as they were passing yesterday and the recent changes were just about renaming? |
|
@felixarntz I've spent the morning tracking down why the tests started failing, and it looks like moving the file into the themes directory exposed an existing problem with our test suite, where development mode had gotten set to 'theme' (which causes the caching we're testing in this file to return |
felixarntz
left a comment
There was a problem hiding this comment.
Thanks @joemcgill, that makes sense. I had run into a similar situation the other day when working on #5467, I should have considered to fix this holistically 🙃
One point of feedback below, we should rather unset the global than setting it to an empty string to clean up global state, but that's only a minor issue that could be fixed pre-commit.
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
This caches block patterns using
WP_Object_Cacherather than transients. Caches are stored in the global group so sites with persistent caches can still get the performance benefit, while allowing this data to be cleared with a standard cache flush, while avoiding persistence issues for sites not using a persistent object cache.Trac ticket: https://core.trac.wordpress.org/ticket/59633
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.