From 80fbb3b27dc28a435b3b4e35eb315d0bc216e77e Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 18 Apr 2008 19:00:07 +0000 Subject: [PATCH] Consult the cached user object in get_usermeta to avoid queries. git-svn-id: http://svn.automattic.com/wordpress/trunk@7741 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/user.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/wp-includes/user.php b/wp-includes/user.php index 15da2386ac..a8445ce180 100644 --- a/wp-includes/user.php +++ b/wp-includes/user.php @@ -148,11 +148,16 @@ function get_usermeta( $user_id, $meta_key = '') { if ( !empty($meta_key) ) { $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); - $metas = $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) ); + $user = wp_cache_get($user_id, 'users'); + // Check the cached user object + if ( false !== $user && isset($user->$meta_key) ) + $metas = array($user->$meta_key); + else + $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) ); } else { - $metas = $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = %d", $user_id) ); + $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d", $user_id) ); } - + error_log("$meta_key: " . var_export($metas, true), 0); if ( empty($metas) ) { if ( empty($meta_key) ) return array(); @@ -160,13 +165,12 @@ function get_usermeta( $user_id, $meta_key = '') { return ''; } - foreach ($metas as $meta) - $values[] = maybe_unserialize($meta->meta_value); + $metas = array_map('maybe_unserialize', $metas); - if ( count($values) == 1 ) - return $values[0]; + if ( count($metas) == 1 ) + return $metas[0]; else - return $values; + return $metas; } function update_usermeta( $user_id, $meta_key, $meta_value ) {