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
This commit is contained in:
ryan 2008-04-18 19:00:07 +00:00
parent 2ca92d8905
commit 80fbb3b27d
1 changed files with 12 additions and 8 deletions

View File

@ -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 ) {