update_network_cache()
云策文档标注
概述
update_network_cache() 函数用于更新指定网络的缓存,将网络对象添加到缓存中,避免重复更新已存在的网络ID。
关键要点
- 函数功能:将网络对象数组添加到缓存,使用网络ID作为键,分组为 'networks'。
- 参数要求:$networks 参数为必需的网络行对象数组。
- 缓存行为:如果网络ID已存在于缓存中,则不会更新,确保数据一致性。
- 内部实现:通过 wp_cache_add_multiple() 批量添加数据到缓存。
代码示例
function update_network_cache( $networks ) {
$data = array();
foreach ( (array) $networks as $network ) {
$data[ $network->id ] = $network;
}
wp_cache_add_multiple( $data, 'networks' );
}注意事项
- 此函数从 WordPress 4.6.0 版本引入。
- 相关函数:wp_cache_add_multiple() 用于批量缓存操作,prime_network_caches() 用于预加载网络缓存。
原文内容
Updates the network cache of given networks.
Description
Will add the networks in $networks to the cache. If network ID already exists in the network cache then it will not be updated. The network is added to the cache using the network group with the key using the ID of the networks.
Parameters
$networksarrayrequired-
Array of network row objects.
Source
function update_network_cache( $networks ) {
$data = array();
foreach ( (array) $networks as $network ) {
$data[ $network->id ] = $network;
}
wp_cache_add_multiple( $data, 'networks' );
}
Changelog
| Version | Description |
|---|---|
| 4.6.0 | Introduced. |