I felt a bit stupid on Saturday when I came across this nice chunk of code:
I'm looking to find 18 random friends of a user to display on their homepage.
So why is this so stupid? This bit of code runs on every member page including friend pages and is a fairly slow query. Django QuerySets are lazy, meaning that they aren't evaluated until their data is needed. So I wasn't really caching the result of this query at all. Here's my current fix:
If anyone has any better suggestions, please let me know. I'm especially interested in hearing a runtime comparison of sorting by MySQL to Python random.shuffle to list.pop. Say the average user has 100 friends and the limit is usually 18 random friends.
Hmmm. Perhaps Pownce just needs a Top 18.