My company, adPickles, needs to scale big. According to our estimates the site will have to serve pages into the series of tubes faster than the trucks will be able to carry them. Yeah. Something like 3-17 trucks per tube*. That fast.
So we’ve been keeping an eye on various types of caching. One of my favorites is simple method memoization of the kind we’ve all seen before:
1 2 3 4 |
def last_user @last_user ||= User.something_Im_too_lazy_to_make_up end |
Which is great. As long as you don’t need that value to be updated for the life of the object it’s in that’s a solid way to prevent the value from being calculated twice.
But it can get pretty ugly when you have a multi-line method:1 2 3 4 5 6 7 |
def current_advertising_balance return @current_advertising_balance if @current_advertising_balance amount_owed = Invoice.procces.something(:complicated) + OtherThing amount_paid = Payment.procces.something(:complicated) + OtherThing @current_advertising_balance ||= amount_owed - amount_paid end |
It gets the job done just fine but it’s a hack. You’re using @current_advertising_balance in three (3!) places in the same method.
I just came across this easier way. I don’t know where I found this but I haven’t been using it until lately and I’m really starting to like it. Check this out:
Update: Thanks to Mourad for pointing out the missing ’||=’1 2 3 4 5 6 7 8 |
def current_advertising_balance @current_advertising_balance ||= begin amount_owed = Invoice.procces.something(:complicated) + OtherThing amount_paid = Payment.procces.something(:complicated) + OtherThing amount_owed - amount_paid end end |
1 2 3 4 5 6 7 8 9 10 11 12 |
def current_advertising_balance @current_advertising_balance ||= begin amount_owed = Invoice.procces.something(:complicated) + OtherThing amount_paid = Payment.procces.something(:complicated) + OtherThing amount_owed - amount_paid rescue 0.0 ensure Advertiser.mark_that_we_calculated_balance end end |
- 1 truck per tube is equivalent to 1K requests per second
6 responses so far ↓
1 Ted // Apr 05, 2008 at 04:34 AM
When I first read that, I thought to myself, “but he’s not caching anymore; he’s recalculating every time the variable is referenced!” But I cracked open irb and tried it myself and sure enough it only evaluates once. Pretty slick.
teflon-ted:~/Desktop/adPickles/rails ted$ irb >> a = 2 => 2 >> b = 3 => 3 >> m = begin a + b end => 5 >> a = 7 => 7 >> m => 5
Of course that means your “current_advertising_balance” is only “current” from the time that it was first evaluated ;-)
2 Ted // Apr 05, 2008 at 04:35 AM
Apologies for the poorly formatted prior post – gawd I hate forms without preview buttons :-(
3 Mourad // Apr 05, 2008 at 06:53 AM
Hi
I think you have typo in your last examples It should be :For Ted I your example the value of m is not modified because m contains the result of the calculation and not the calculation itself ;-)
4 Jack Danger // Apr 05, 2008 at 07:19 AM
Thanks for the proofreading Mourad, you’re a big help :-)
5 Daniel Fischer // Apr 05, 2008 at 05:30 PM
Wow, that is an amazing pattern to uphold to. So many plusses all around it! Thanks for sharing that Danger!
6 Ryan Bates // Apr 07, 2008 at 03:03 PM
Nice tip. I usually move it into a 2nd method prefixed with “calculate”. In this example I would create a new private method called “calculate_current_advertising_balance”. This way the “current_advertising_balance” method simply handles the caching and calling of the calculate method.
Leave a Comment