aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/query_cache.rb
blob: e25335ecea0729b5d5eeb7e091bb702c6c9c0243 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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)
      cache(sql) { @connection.select_all(sql, name) }
    end

    def select_one(sql, name = nil)
      cache(sql) { @connection.select_one(sql, name) }
    end
    
    def select_values(sql, name = nil)
      cache(sql) { @connection.select_values(sql, name) }
    end

    def select_value(sql, name = nil)
      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 cache(sql)
        result = if @query_cache.has_key?(sql)
          log_info(sql, "CACHE", 0.0)
          @query_cache[sql]
        else
          @query_cache[sql] = yield
        end

        if result
          # perform a deep #dup in case result is an array
          result = result.collect { |row| row.dup } if result.is_a?(Array)
          result.dup
        else
          nil
        end
      end
    
      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_#{connection_without_query_cache.object_id}"] ||= {}
      end
      
      def query_cache
        if query_caches[self]
          query_caches[self]
        elsif superclass.respond_to?(:query_cache) and superclass.respond_to?(:connection) and superclass.connection_without_query_cache == connection_without_query_cache
          superclass.query_cache
        end
      end
      
      def query_cache=(cache)
        query_caches[self] = cache
      end

      # Use a query cache within the given block.
      def cache
        # Don't cache if Active Record is not configured.
        if ActiveRecord::Base.configurations.blank?
          yield
        else
          begin
            self.query_cache = QueryCache.new(connection_without_query_cache)
            yield
          ensure
            self.query_cache = nil
          end
        end
      end

      def connection
        query_cache || connection_without_query_cache
      end
    end
  end  
end