Check cache return value in metadata_exists(), get_metadata_raw(), an… - #13481
josephscott wants to merge 17 commits into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Co-authored-by: Jon Surrell <sirreal@users.noreply.github.com>
There was a problem hiding this comment.
This is looking good but I've suggested a few items inline.
While testing, I added this to my test suite to ensure primed empty caches didn't result in a database query. It might be worth adding to the suite:
public function test_get_metadata_hits_cache_when_no_meta_data_set() {
$post_id = $this->factory()->post->create();
$post_meta = get_post_meta( $post_id );
// Delete all the meta data.
foreach ( $post_meta as $meta_key => $meta_value ) {
delete_post_meta( $post_id, $meta_key );
}
// Ensure the cache is flushed.
wp_cache_delete( $post_id, 'post_meta' );
$start_queries = get_num_queries();
$meta_data = get_post_meta( $post_id );
$this->assertSame( 1, get_num_queries() - $start_queries, 'Expected database query getting meta with flushed cache' );
$this->assertSame( array(), $meta_data, 'Post meta is expected to be an empty array' );
$start_queries = get_num_queries();
$meta_data = get_post_meta( $post_id );
$this->assertSame( 0, get_num_queries() - $start_queries, 'Expected no database query getting meta with primed cache' );
}|
|
||
| if ( ! $meta_cache ) { | ||
| // A cached value that is not an array is unusable, treat it as a cache miss. | ||
| if ( ! $meta_cache || ! is_array( $meta_cache ) ) { |
There was a problem hiding this comment.
It's not directly related but a valid cache of an empty array will follow the update_meta_cache() flow. update_meta_cache() will detect that the cache is primed so it's not a big deal.
Do you think it's worth including that as part of the fix for this ticket?
There was a problem hiding this comment.
I don't think that is worth including for this PR - where the focus was on avoiding fatal error conditions. I'd rather see that happen in a separate PR.
There was a problem hiding this comment.
I'd probably include test_get_metadata_hits_cache_when_no_meta_data_set() in a separate PR as well, associated with empty cache array idea.
peterwilsoncc
left a comment
There was a problem hiding this comment.
Thanks @josephscott, these update all look good to me.
I don't think that is worth including for this PR - where the focus was on avoiding fatal error conditions. I'd rather see that happen in a separate PR.
Works for me.
Check cache return value in
metadata_exists(),get_metadata_raw(), andupdate_meta_cache()Trac ticket: https://core.trac.wordpress.org/ticket/66091
AI assistance: Yes
Tool(s): Claude Code
Model(s): Fable 5.1
Used for: talking over the general issue, exploring options - it wrote all of the tests.
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.