query_cache.rb 2.7 KB
Newer Older
1 2 3 4
module ActiveRecord
  module ConnectionAdapters # :nodoc:
    module QueryCache
      class << self
5
        def included(base) #:nodoc:
6 7 8 9 10
          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(arel, name = nil, binds = [])
59
        if @query_cache_enabled
60
          sql = to_sql(arel, binds)
61
          cache_sql(sql, binds) { super(sql, name, binds) }
62
        else
63
          super
64 65 66 67
        end
      end

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

78 79 80 81 82
        # FIXME: we should guarantee that all cached items are Result
        # objects.  Then we can avoid this conditional
        if ActiveRecord::Result === result
          result.dup
        else
83
          result.collect { |row| row.dup }
84
        end
85
      end
86 87 88
    end
  end
end