[wp-trac] [WordPress Trac] #61467: Inconsistent cache handling for network (site) meta
WordPress Trac
noreply at wordpress.org
Thu Jun 20 10:08:13 UTC 2024
#61467: Inconsistent cache handling for network (site) meta
--------------------------+-------------------------------------
Reporter: xParham | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Cache API | Version:
Severity: major | Resolution:
Keywords: | Focuses: multisite, performance
--------------------------+-------------------------------------
Comment (by narenin):
Hi @xParham ,
I am also able to replicate the same issue. The issue arises from the
inconsistent use of different WordPress functions for updating site
(network) options and metadata. These different functions use distinct
caching mechanisms, leading to out-of-sync values between
`update_network_option()`/`get_network_option()` and
`update_metadata()`/`get_metadata()`.
**Understanding the Functions and Their Cache Groups**
`update_site_option()` / `update_network_option()`:
**Cache Key:** $network_id:$option
**Cache Group:** site-options
**Usage:** Typically used for storing network-wide options.
update_metadata('site', ...):
**Cache Key:** $object_id (where $object_id is the network ID)
**Cache Group:** site_meta
**Usage:** Typically used for more granular metadata associated with the
site (network).
**Why This Happens**
When you update a network option using update_network_option(), it sets a
value in the site-options cache group. However, if you then update the
same value using `update_metadata('site', ...)`, it sets a value in the
site_meta cache group, bypassing the site-options cache group.
This discrepancy means that `get_network_option()` may retrieve stale data
because it checks the site-options cache group, which hasn't been updated
by `update_metadata('site', ...)`.
**Steps to Reproduce**
Set value using `update_network_option()`:
{{{
wp eval 'update_network_option( 1, "mykey", "123" );'
}}}
**Cache Key:** 1:mykey in site-options
Retrieve value `using get_network_option()`:
{{{
wp eval 'echo get_network_option( 1, "mykey" );'
}}}
**Output:** 123
Set value using `update_metadata('site', ...)`:
{{{
wp network meta update 1 mykey 456
}}}
**Cache Key:** 1 in site_meta
Retrieve value using `get_metadata('site', ...)`:
{{{
wp network meta get 1 mykey
}}}
**Output:** 456
Retrieve value again using `get_network_option()`:
{{{
wp eval 'echo get_network_option( 1, "mykey" );'
}}}
**Output:** 123 (still, because the site-options cache was not updated)
**Solution: Invalidate Caches**
One solution to ensure consistency is to manually invalidate or update
both caches when you change a network option or metadata.
**Example Code to Synchronize Caches:**
{{{#!php
<?php
function sync_network_option_and_meta($network_id, $key, $value) {
// Update using update_network_option()
update_network_option($network_id, $key, $value);
// Update the metadata as well
update_metadata('site', $network_id, $key, $value);
// Clear both caches
wp_cache_delete("$network_id:$key", 'site-options');
wp_cache_delete($network_id, 'site_meta');
}
}}}
// Usage in WP CLI
{{{
wp eval 'sync_network_option_and_meta(1, "mykey", "456");'
}}}
**Ensuring Consistency in WP CLI Scripts**
When writing WP CLI scripts or commands, ensure that any update to a
network option also updates the corresponding metadata and clears the
relevant caches.
**
WP CLI Command Example**
{{{
wp eval 'function sync_network_option_and_meta($network_id, $key, $value)
{
// Update using update_network_option()
update_network_option($network_id, $key, $value);
// Update the metadata as well
update_metadata("site", $network_id, $key, $value);
// Clear both caches
wp_cache_delete("$network_id:$key", "site-options");
wp_cache_delete($network_id, "site_meta");
}
sync_network_option_and_meta(1, "mykey", "456");'
}}}
By ensuring that both the network option and site meta are updated and
their respective caches are cleared, we can maintain consistent and
synchronized values across our WordPress network.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/61467#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list