$args = array(
'count' => true,
);
$terms = get_terms( $args );
wp_cache_set()
$cache_key = 'term_post_counts';
$counts = get_transient($cache_key);
if ($counts === false) {
$counts = $wpdb->get_results($sql);
set_transient($cache_key, $counts, HOUR_IN_SECONDS);
}