query_cache.rb 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
module ActiveRecord
  module ConnectionAdapters # :nodoc:
    module QueryCache
      class << self
        def included(base)
          dirties_query_cache base, :insert, :update, :delete
        end

        def dirties_query_cache(base, *method_names)
          method_names.each do |method_name|
11
            base.class_eval <<-end_code, __FILE__, __LINE__ + 1
12 13 14 15
              def #{method_name}(*)                         # def update_with_query_dirty(*args)
                clear_query_cache if @query_cache_enabled   #   clear_query_cache if @query_cache_enabled
                super                                       #   update_without_query_dirty(*args)
              end                                           # end
16 17 18 19 20
            end_code
          end
        end
      end

21
      attr_reader :query_cache, :query_cache_enabled
N
Nick Sieger 已提交
22

23 24
      # Enable the query cache within the block.
      def cache
25
        old, @query_cache_enabled = @query_cache_enabled, true
26 27 28
        yield
      ensure
        clear_query_cache
29
        @query_cache_enabled = old
30 31
      end

32 33 34 35 36 37 38 39
      def enable_query_cache!
        @query_cache_enabled = true
      end

      def disable_query_cache!
        @query_cache_enabled = false
      end

40 41
      # Disable the query cache within the block.
      def uncached
42
        old, @query_cache_enabled = @query_cache_enabled, false
43 44
        yield
      ensure
45
        @query_cache_enabled = old
46 47
      end

P
Pratik Naik 已提交
48 49 50 51 52 53
      # Clears the query cache.
      #
      # One reason you may wish to call this method explicitly is between queries
      # that ask the database to randomize results. Otherwise the cache would see
      # the same SQL query and repeatedly return the same result each time, silently
      # undermining the randomness you were expecting.
54
      def clear_query_cache
55
        @query_cache.clear
56 57
      end

58
      def select_all(sql, name = nil, binds = [])
59
        if @query_cache_enabled
60
          cache_sql(sql, binds) { super }
61
        else
62
          super
63 64 65 66
        end
      end

      private
67
        def cache_sql(sql, binds)
68
          result =
A
Aaron Patterson 已提交
69
            if @query_cache[sql].key?(binds)
70
              ActiveSupport::Notifications.instrument("sql.active_record",
A
Aaron Patterson 已提交
71
                :sql => sql, :name => "CACHE", :connection_id => object_id)
A
Aaron Patterson 已提交
72
              @query_cache[sql][binds]
73
            else
A
Aaron Patterson 已提交
74
              @query_cache[sql][binds] = yield
75 76
            end

77
          result.collect { |row| row.dup }
78 79 80 81
        end
    end
  end
end