Index: wp-includes/category.php
===================================================================
--- wp-includes/category.php	(revision 21762)
+++ wp-includes/category.php	(working copy)
@@ -14,12 +14,8 @@
  * @return object List of all of the category IDs.
  */
 function get_all_category_ids() {
-	if ( ! $cat_ids = wp_cache_get( 'all_category_ids', 'category' ) ) {
-		$cat_ids = get_terms( 'category', array('fields' => 'ids', 'get' => 'all') );
-		wp_cache_add( 'all_category_ids', $cat_ids, 'category' );
-	}
-
-	return $cat_ids;
+	// this call returns from cache if present
+	return get_terms( 'category', array( 'fields' => 'ids', 'get' => 'all' ) );
 }
 
 /**
@@ -36,7 +32,7 @@
  * @param string|array $args Optional. Change the defaults retrieving categories.
  * @return array List of categories.
  */
-function &get_categories( $args = '' ) {
+function get_categories( $args = '' ) {
 	$defaults = array( 'taxonomy' => 'category' );
 	$args = wp_parse_args( $args, $defaults );
 
@@ -78,7 +74,7 @@
  * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
  * @return mixed Category data in type defined by $output parameter.
  */
-function &get_category( $category, $output = OBJECT, $filter = 'raw' ) {
+function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
 	$category = get_term( $category, 'category', $output, $filter );
 	if ( is_wp_error( $category ) )
 		return $category;
@@ -249,7 +245,7 @@
  * @param string|array $args Tag arguments to use when retrieving tags.
  * @return array List of tags.
  */
-function &get_tags( $args = '' ) {
+function get_tags( $args = '' ) {
 	$tags = get_terms( 'post_tag', $args );
 
 	if ( empty( $tags ) ) {
@@ -280,7 +276,7 @@
  * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
  * @return object|array Return type based on $output value.
  */
-function &get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
+function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
 	return get_term( $tag, 'post_tag', $output, $filter );
 }
 
Index: wp-includes/taxonomy.php
===================================================================
--- wp-includes/taxonomy.php	(revision 21762)
+++ wp-includes/taxonomy.php	(working copy)
@@ -853,13 +853,13 @@
  * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
  *
  * @param int|object $term If integer, will get from database. If object will apply filters and return $term.
- * @param string $taxonomy Taxonomy name that $term is part of.
+ * @param string $taxonomy Optional if object is passed for $term. Taxonomy name that $term is part of.
  * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
  * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
  * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not
  * exist then WP_Error will be returned.
  */
-function &get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
+function get_term($term, $taxonomy = '', $output = OBJECT, $filter = 'raw') {
 	global $wpdb;
 	$null = null;
 
@@ -868,24 +868,28 @@
 		return $error;
 	}
 
-	if ( ! taxonomy_exists($taxonomy) ) {
-		$error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
-		return $error;
-	}
+	if ( is_object( $term ) )
+		$taxonomy = $term->taxonomy;
 
+	if ( ! taxonomy_exists( $taxonomy ) )
+		return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );	
+
 	if ( is_object($term) && empty($term->filter) ) {
-		wp_cache_add($term->term_id, $term, $taxonomy);
+		update_term_cache( $term, $taxonomy );
 		$_term = $term;
 	} else {
 		if ( is_object($term) )
 			$term = $term->term_id;
+		
 		if ( !$term = (int) $term )
 			return $null;
-		if ( ! $_term = wp_cache_get($term, $taxonomy) ) {
+		
+		if ( ! $_term = wp_cache_get( $term, term_cache_group( $taxonomy ) ) ) {
 			$_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND t.term_id = %d LIMIT 1", $taxonomy, $term) );
 			if ( ! $_term )
 				return $null;
-			wp_cache_add($term, $_term, $taxonomy);
+			
+			update_term_cache( $_term, $taxonomy );
 		}
 	}
 
@@ -940,18 +944,26 @@
 		return false;
 
 	if ( 'slug' == $field ) {
-		$field = 't.slug';
-		$value = sanitize_title($value);
-		if ( empty($value) )
+		$value = sanitize_title( $value );
+		if ( empty( $value ) )
 			return false;
+		
+		$term_id = wp_cache_get( $value, term_cache_group( $taxonomy, $field ) );
+		$field = 't.slug';		
 	} else if ( 'name' == $field ) {
 		// Assume already escaped
-		$value = stripslashes($value);
-		$field = 't.name';
+		$value = stripslashes( $value );
+		$term_id = wp_cache_get( $value, term_cache_group( $taxonomy, $field ) );
+		$field = 't.name';		
 	} else {
-		$term = get_term( (int) $value, $taxonomy, $output, $filter);
+		$term_id = $value;
+	}
+	
+	if ( ! empty( $term_id ) ) {
+		$term = get_term( (int) $term_id, $taxonomy, $output, $filter );
 		if ( is_wp_error( $term ) )
 			$term = false;
+		
 		return $term;
 	}
 
@@ -959,7 +971,7 @@
 	if ( !$term )
 		return false;
 
-	wp_cache_add($term->term_id, $term, $taxonomy);
+	update_term_cache( $term, $taxonomy );
 
 	$term = apply_filters('get_term', $term, $taxonomy);
 	$term = apply_filters("get_$taxonomy", $term, $taxonomy);
@@ -1176,7 +1188,7 @@
  * @param string|array $args The values of what to search for when returning terms
  * @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist.
  */
-function &get_terms($taxonomies, $args = '') {
+function get_terms($taxonomies, $args = '') {
 	global $wpdb;
 	$empty_array = array();
 
@@ -1232,8 +1244,11 @@
 	}
 
 	// $args can be whatever, only use the args defined in defaults to compute the key
-	$filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
-	$key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key );
+	// deprecate 'list_terms_exclusions' - use 'get_terms' filter instead
+	if ( has_filter( 'list_terms_exclusions' ) )
+		_doing_it_wrong( __FUNCTION__, __( '"list_terms_exclusions" is no longer supported. Use the "get_terms" filter instead.' ), '3.5' );
+	
+	$key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) );
 	$last_changed = wp_cache_get('last_changed', 'terms');
 	if ( !$last_changed ) {
 		$last_changed = time();
@@ -2556,7 +2571,7 @@
 
 	foreach ( $object_ids as $id )
 		foreach ( get_object_taxonomies($object_type) as $taxonomy )
-			wp_cache_delete($id, "{$taxonomy}_relationships");
+			wp_cache_delete( $id, term_cache_group( $taxonomy, 'relationships' ) );
 
 	do_action('clean_object_term_cache', $object_ids, $object_type);
 }
@@ -2579,28 +2594,31 @@
 
 	if ( !is_array($ids) )
 		$ids = array($ids);
-
+	
 	$taxonomies = array();
+	
+	$select = "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
+	
+	$ids = array_map( 'intval', $ids );
 	// If no taxonomy, assume tt_ids.
-	if ( empty($taxonomy) ) {
-		$tt_ids = array_map('intval', $ids);
-		$tt_ids = implode(', ', $tt_ids);
-		$terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)");
+	if ( empty( $taxonomy ) ) {
+		$terms = $wpdb->get_results( $wpdb->prepare( "$select WHERE tt.term_taxonomy_id IN (" . join( ',', $ids ) . ")" ) );
 		$ids = array();
-		foreach ( (array) $terms as $term ) {
+		foreach ( $terms as $term ) {
 			$taxonomies[] = $term->taxonomy;
 			$ids[] = $term->term_id;
-			wp_cache_delete($term->term_id, $term->taxonomy);
 		}
 		$taxonomies = array_unique($taxonomies);
 	} else {
-		$taxonomies = array($taxonomy);
-		foreach ( $taxonomies as $taxonomy ) {
-			foreach ( $ids as $id ) {
-				wp_cache_delete($id, $taxonomy);
-			}
-		}
+		$terms = $wpdb->get_results( $wpdb->prepare( "$select WHERE tt.taxonomy = %s AND tt.term_id IN (" . join( ',', $ids ) . ")", $taxonomy ) );
+		$taxonomies = array( $taxonomy );		
 	}
+	
+ 	foreach ( $terms as $term ) {
+		wp_cache_delete( $term->term_id, term_cache_group( $term->taxonomy ) );
+		wp_cache_delete( $term->name, term_cache_group( $term->taxonomy, 'names' ) );
+		wp_cache_delete( $term->slug, term_cache_group( $term->taxonomy, 'slugs' ) );	
+	}
 
 	foreach ( $taxonomies as $taxonomy ) {
 		if ( isset($cleaned[$taxonomy]) )
@@ -2608,8 +2626,6 @@
 		$cleaned[$taxonomy] = true;
 
 		if ( $clean_taxonomy ) {
-			wp_cache_delete('all_ids', $taxonomy);
-			wp_cache_delete('get', $taxonomy);
 			delete_option("{$taxonomy}_children");
 			// Regenerate {$taxonomy}_children
 			_get_term_hierarchy($taxonomy);
@@ -2634,8 +2650,8 @@
  * @param string $taxonomy Taxonomy Name
  * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id.
  */
-function &get_object_term_cache($id, $taxonomy) {
-	$cache = wp_cache_get($id, "{$taxonomy}_relationships");
+function get_object_term_cache($id, $taxonomy) {
+	$cache = wp_cache_get( $id, term_cache_group( $taxonomy, 'relationships' ) );
 	return $cache;
 }
 
@@ -2675,7 +2691,7 @@
 	$ids = array();
 	foreach ( (array) $object_ids as $id ) {
 		foreach ( $taxonomies as $taxonomy ) {
-			if ( false === wp_cache_get($id, "{$taxonomy}_relationships") ) {
+			if ( false === wp_cache_get( $id, term_cache_group( $taxonomy, 'relationships' ) ) ) {
 				$ids[] = $id;
 				break;
 			}
@@ -2703,7 +2719,7 @@
 
 	foreach ( $object_terms as $id => $value ) {
 		foreach ( $value as $taxonomy => $terms ) {
-			wp_cache_add( $id, $terms, "{$taxonomy}_relationships" );
+			wp_cache_add( $id, $terms, term_cache_group( $taxonomy, 'relationships' ) );
 		}
 	}
 }
@@ -2718,16 +2734,73 @@
  * @param array $terms List of Term objects to change
  * @param string $taxonomy Optional. Update Term to this taxonomy in cache
  */
-function update_term_cache($terms, $taxonomy = '') {
-	foreach ( (array) $terms as $term ) {
-		$term_taxonomy = $taxonomy;
-		if ( empty($term_taxonomy) )
-			$term_taxonomy = $term->taxonomy;
+function update_term_cache( $terms, $taxonomy = '' ) {
+	if ( ! is_array( $terms ) )
+		$terms = array( $terms );
+	
+	foreach ( $terms as $term ) {
+		$tax = $taxonomy;
+		if ( empty( $tax ) )
+			$tax = $term->taxonomy;
 
-		wp_cache_add($term->term_id, $term, $term_taxonomy);
+		wp_cache_add( $term->term_id, $term, term_cache_group( $tax ) );
+		wp_cache_add( $term->name, $term->term_id, term_cache_group( $tax, 'names' ) );
+		wp_cache_add( $term->slug, $term->term_id, term_cache_group( $tax, 'slugs' ) );
 	}
 }
 
+/**
+ *
+ * @package WordPress
+ * @subpackage Taxonomy
+ * @since 3.5.0
+ *
+ * @param string $taxonomy Taxonomy
+ * @param string $group Optional. ids, names, slugs, or relationships.
+ */
+function term_cache_group( $taxonomy, $group = 'ids' ) {
+	$tax = get_taxonomy( $taxonomy );
+	
+	if ( ! $tax )
+		return false;
+
+	if ( $tax->_builtin ) {
+		$key = $taxonomy;
+		switch ( $group ) {
+			case 'name':
+			case 'names':
+				$key = "$taxonomy:names";
+				break;
+			case 'slug':
+			case 'slugs':
+				$key = "$taxonomy:slugs";
+				break;
+			case 'relationship':
+			case 'relationships':
+				$key = "{$taxonomy}_relationships";
+				break;			
+		}
+	} else {
+		$key = "taxonomy:$taxonomy:ids";
+		switch ( $group ) {
+			case 'name':
+			case 'names':
+				$key = "taxonomy:$taxonomy:names";
+				break;
+			case 'slug':
+			case 'slugs':
+				$key = "taxonomy:$taxonomy:slugs";
+				break;
+			case 'relationship':
+			case 'relationships':
+				$key = "taxonomy:$taxonomy:relationships";
+				break;			
+		}
+	}
+	
+	return $key;	
+}
+
 //
 // Private
 //
@@ -2780,7 +2853,7 @@
  * @param string $taxonomy The taxonomy which determines the hierarchy of the terms.
  * @return array The subset of $terms that are descendants of $term_id.
  */
-function &_get_term_children($term_id, $terms, $taxonomy) {
+function _get_term_children($term_id, $terms, $taxonomy) {
 	$empty_array = array();
 	if ( empty($terms) )
 		return $empty_array;
Index: wp-includes/media.php
===================================================================
--- wp-includes/media.php	(revision 21762)
+++ wp-includes/media.php	(working copy)
@@ -1156,8 +1156,6 @@
 	 * @uses WP_Embed::maybe_make_link()
 	 * @uses get_option()
 	 * @uses current_user_can()
-	 * @uses wp_cache_get()
-	 * @uses wp_cache_set()
 	 * @uses get_post_meta()
 	 * @uses update_post_meta()
 	 *
Index: wp-includes/category-template.php
===================================================================
--- wp-includes/category-template.php	(revision 21762)
+++ wp-includes/category-template.php	(working copy)
@@ -1065,7 +1065,7 @@
 	$terms = get_object_term_cache( $post->ID, $taxonomy );
 	if ( false === $terms ) {
 		$terms = wp_get_object_terms( $post->ID, $taxonomy );
-		wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
+		wp_cache_add( $post->ID, $terms, term_cache_group( $taxonomy, 'relationships' ) );
 	}
 
 	$terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );
