Sha256: bee43bf96d7b27908a6edc91ca029052f74314347a760160c3f5623c68d73c6a

Contents?: true

Size: 1.83 KB

Versions: 33

Compression:

Stored size: 1.83 KB

Contents

require 'active_support/core_ext/object/blank'

module ActiveRecord
  # = Active Record Query Cache
  class QueryCache
    module ClassMethods
      # Enable the query cache within the block if Active Record is configured.
      def cache(&block)
        if ActiveRecord::Base.configurations.blank?
          yield
        else
          connection.cache(&block)
        end
      end

      # Disable the query cache within the block if Active Record is configured.
      def uncached(&block)
        if ActiveRecord::Base.configurations.blank?
          yield
        else
          connection.uncached(&block)
        end
      end
    end

    def initialize(app)
      @app = app
    end

    class BodyProxy # :nodoc:
      def initialize(original_cache_value, target)
        @original_cache_value = original_cache_value
        @target               = target
      end

      def method_missing(method_sym, *arguments, &block)
        @target.send(method_sym, *arguments, &block)
      end

      def respond_to?(method_sym, include_private = false)
        super || @target.respond_to?(method_sym)
      end

      def each(&block)
        @target.each(&block)
      end

      def close
        @target.close if @target.respond_to?(:close)
      ensure
        ActiveRecord::Base.connection.clear_query_cache
        unless @original_cache_value
          ActiveRecord::Base.connection.disable_query_cache!
        end
      end
    end

    def call(env)
      old = ActiveRecord::Base.connection.query_cache_enabled
      ActiveRecord::Base.connection.enable_query_cache!

      status, headers, body = @app.call(env)
      [status, headers, BodyProxy.new(old, body)]
    rescue Exception => e
      ActiveRecord::Base.connection.clear_query_cache
      unless old
        ActiveRecord::Base.connection.disable_query_cache!
      end
      raise e
    end
  end
end

Version data entries

33 entries across 33 versions & 2 rubygems

Version Path
dirty_history-0.7.3 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.7.2 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.7.1 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.7.0 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.6.7 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.6.6 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.6.5 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.6.4 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.6.3 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.6.2 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.6.1 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.6.0 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.5.4 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.5.3 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.5.2 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.5.1 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.5.0 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.4.10 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.4.9 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb
dirty_history-0.4.8 dirty_history/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/query_cache.rb