jnewland / lazy_record

Proof of concept Lazy-Loading for ActiveRecord. Inspired by the 'kickers' of Ambition.

This URL has Read+Write access

lazy_record / README
100644 49 lines (35 sloc) 1.57 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
LazyRecord
==========
 
Proof of concept Lazy-Loading for ActiveRecord. Inspired by the 'kickers' of Ambition.
  
  >> b = Buzz.lazy_find(:first)
  => #<ActiveRecord::Lazy::Promise computation=#<Proc:0x025d1e50@...>>
  -------------No SQL query is run until a method is called on this 'Promise'
  >> b.to_s
  -------------Buzz Load (0.000578) SELECT * FROM buzz LIMIT 1
  => "Inaugural Buzz"
  
Use the +lazy_record+ class method to make this the default for a certain class:
  
  class Buzz << ActiveRecord::Base
    lazy_record
  end
  
  >> b = Buzz.find(:first)
  => #<ActiveRecord::Lazy::Promise computation=#<Proc:0x025d1e50@...>>
  -------------No SQL query is run until a method is called on this 'Promise'
  >> b.to_s
  -------------Buzz Load (0.000578) SELECT * FROM buzz LIMIT 1
  => "Inaugural Buzz"
 
 
Why you might want to use this
===========
 
Say you've got some kick-ass cache_fu going on in your views - huge blocks of HTML being cached with a TTL of 30 mins or so.
But, each hit on your controller still fires off the 'spensive DB queries to fetch your tag cloud. With lazy loading, these
queries aren't run until absolutely necessary - giving your DB a rest til your cache expires, and boosting your reqs/sec.
 
 
Why you might not want to use this
===========
 
  >> b = Buzz.lazy_find(123023424)
  => #<ActiveRecord::Lazy::Promise computation=#<Proc:0x025d1e50@...>>
  >> puts "booleans are screwed" if b
  booleans are screwed
 
 
Promise code from here: http://moonbase.rydia.net/software/lazy.rb/
 
Contact
=======
Jesse Newland
jnewland@gmail.com