efficient way to query for count totals
SELECT friend_id, COUNT(*)
    FROM num_friends
    GROUP BY friend_id;


This will assist in making it fast:

INDEX(friend_id)


With that index, you will find out that the original query is so fast that you don't need to cache the results.