Skip to content

Commit

Permalink
Update/prime/clear alias-meta caches.
Browse files Browse the repository at this point in the history
  • Loading branch information
JJJ committed Apr 7, 2017
1 parent 3280d0d commit 040a9e2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
21 changes: 16 additions & 5 deletions wp-site-aliases/includes/functions/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@
* @see update_site_cache()
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $ids ID list.
* @param array $ids ID list.
* @param bool $update_meta_cache Whether to update site alias cache. Default true.
*/
function _prime_site_alias_caches( $ids = array() ) {
function _prime_site_alias_caches( $ids = array(), $update_meta_cache = true ) {
global $wpdb;

$non_cached_ids = _get_non_cached_ids( $ids, 'blog-aliases' );
if ( ! empty( $non_cached_ids ) ) {
$fresh_aliases = $wpdb->get_results( sprintf( "SELECT * FROM {$wpdb->blog_aliases} WHERE id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );

update_site_alias_cache( $fresh_aliases );
update_site_alias_cache( $fresh_aliases, $update_meta_cache );
}
}

Expand All @@ -37,19 +38,29 @@ function _prime_site_alias_caches( $ids = array() ) {
*
* @since 1.0.0
*
* @param array $aliases Array of site alias objects.
* @param array $aliases Array of site alias objects.
* @param bool $update_meta_cache Whether to update site alias cache. Default true.
*/
function update_site_alias_cache( $aliases = array() ) {
function update_site_alias_cache( $aliases = array(), $update_meta_cache = true ) {

// Bail if no aliases
if ( empty( $aliases ) ) {
return;
}

// Setup array for IDs
$alias_ids = array();

// Loop through aliases & add them to cache group
foreach ( $aliases as $alias ) {
$alias_ids[] = $alias->id;
wp_cache_add( $alias->id, $alias, 'blog-aliases' );
}

// Maybe update site alias meta cache
if ( true === $update_meta_cache ) {
update_site_aliasmeta_cache( $alias_ids );
}
}

/**
Expand Down
17 changes: 17 additions & 0 deletions wp-site-aliases/includes/functions/metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,20 @@ function get_site_alias_meta( $id, $meta_key = '', $single = false ) {
function update_site_alias_meta( $id, $meta_key, $meta_value, $prev_value = '' ) {
return update_metadata( 'blog_alias', $id, $meta_key, $meta_value, $prev_value );
}

/**
* Updates metadata cache for list of site alias IDs.
*
* Performs SQL query to retrieve the metadata for the site alias IDs and
* updates the metadata cache for the site aliases. Therefore, the functions,
* which call this function, do not need to perform SQL queries on their own.
*
* @since 6.1.0
*
* @param array $ids List of site alias IDs.
* @return array|false Returns false if there is nothing to update or an array
* of metadata.
*/
function update_site_aliasmeta_cache( $ids ) {
return update_meta_cache( 'blog_alias', $ids );
}

0 comments on commit 040a9e2

Please sign in to comment.