aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/query_cache.rb
blob: e2f5c1bd88d6ac5270dacc18e15f0f4b2a9f97a6 (plain) (blame)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
module ActiveRecord
  class QueryCache #:nodoc:
    def initialize(connection)
      @connection = connection
      @query_cache = {}
    end

    def clear_query_cache
      @query_cache.clear
    end

    def select_all(sql, name = nil)
      (@query_cache[sql] ||= @connection.select_all(sql, name)).dup
    end

    def select_one(sql, name = nil)
      @query_cache[sql] ||= @connection.select_one(sql, name)
    end
    
    def select_values(sql, name = nil)
      (@query_cache[sql] ||= @connection.select_values(sql, name)).dup
    end

    def select_value(sql, name = nil)
      @query_cache[sql] ||= @connection.select_value(sql, name)
    end
    
    def execute(sql, name = nil)
      clear_query_cache
      @connection.execute(sql, name)
    end    

    def columns(table_name, name = nil)
      @query_cache["SHOW FIELDS FROM #{table_name}"] ||= @connection.columns(table_name, name)
    end

    def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
      clear_query_cache
      @connection.insert(sql, name, pk, id_value, sequence_name)
    end

    def update(sql, name = nil)
      clear_query_cache
      @connection.update(sql, name)
    end

    def delete(sql, name = nil)
      clear_query_cache
      @connection.delete(sql, name)
    end
    
    private
      def method_missing(method, *arguments, &proc)
        @connection.send(method, *arguments, &proc)
      end
  end
    
  class Base
    # Set the connection for the class with caching on
    class << self
      alias_method :connection_without_query_cache, :connection
      
      def query_caches
        (Thread.current[:query_cache] ||= {})
      end
      
      def query_cache
        if query_caches[self]
          query_caches[self]
        elsif superclass.respond_to?(:query_cache)
          superclass.query_cache
        end
      end
      
      def query_cache=(cache)
        query_caches[self] = cache
      end
            
      def cache        
        self.query_cache = QueryCache.new(connection_without_query_cache)
        yield
      ensure 
        self.query_cache = nil
      end        
      
      def connection
        query_cache || connection_without_query_cache
      end
    end
  end  
end