From 21650c59bca5b85b69c9b03704323c5da4ac5da6 Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 26 Nov 2010 21:35:26 +0000 Subject: [PATCH] Meta data caching improvements. Props mdawaffe. see #15545 git-svn-id: http://svn.automattic.com/wordpress/trunk@16596 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/meta.php | 17 +++++++++-------- wp-includes/user.php | 5 ++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/wp-includes/meta.php b/wp-includes/meta.php index efc0a22a2..849e4339b 100644 --- a/wp-includes/meta.php +++ b/wp-includes/meta.php @@ -259,8 +259,8 @@ function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) { $meta_cache = wp_cache_get($object_id, $meta_type . '_meta'); if ( !$meta_cache ) { - update_meta_cache($meta_type, $object_id); - $meta_cache = wp_cache_get($object_id, $meta_type . '_meta'); + $meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); + $meta_cache = $meta_cache[$object_id]; } if ( !$meta_key ) @@ -309,17 +309,20 @@ function update_meta_cache($meta_type, $object_ids) { $cache_key = $meta_type . '_meta'; $ids = array(); + $cache = array(); foreach ( $object_ids as $id ) { - if ( false === wp_cache_get($id, $cache_key) ) + $cached_object = wp_cache_get( $id, $cache_key ); + if ( false === $cached_object ) $ids[] = $id; + else + $cache[$id] = $cached_object; } if ( empty( $ids ) ) - return false; + return $cache; // Get meta info $id_list = join(',', $ids); - $cache = array(); $meta_list = $wpdb->get_results( $wpdb->prepare("SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list)", $meta_type), ARRAY_A ); @@ -343,11 +346,9 @@ function update_meta_cache($meta_type, $object_ids) { foreach ( $ids as $id ) { if ( ! isset($cache[$id]) ) $cache[$id] = array(); + wp_cache_add( $id, $cache[$id], $cache_key ); } - foreach ( array_keys($cache) as $object) - wp_cache_add($object, $cache[$object], $cache_key); - return $cache; } diff --git a/wp-includes/user.php b/wp-includes/user.php index aa3265d0a..6feb062dc 100644 --- a/wp-includes/user.php +++ b/wp-includes/user.php @@ -1037,10 +1037,9 @@ function get_user_metavalues($ids) { foreach ( $ids as $id ) $objects[$id] = array(); - update_meta_cache('user', $ids); + $metas = update_meta_cache('user', $ids); - foreach ( $ids as $id ) { - $meta = get_metadata('user', $id); + foreach ( $metas as $id => $meta ) { foreach ( $meta as $key => $metavalues ) { foreach ( $metavalues as $value ) { $objects[$id][] = (object)array( 'user_id' => $id, 'meta_key' => $key, 'meta_value' => $value);