Sha256: 0f51ca0dc765173690b8815aa27e9e8387a8da5cf970235b1bc7bf915a5fe374

Contents?: true

Size: 1.17 KB

Versions: 23

Compression:

Stored size: 1.17 KB

Contents

require 'rack/body_proxy'
require 'active_record/query_cache'

module ActiveRecord
  module Turntable
    module Rack
      class QueryCache < ActiveRecord::QueryCache
        def call(env)
          enabled       = ActiveRecord::Base.connection.query_cache_enabled
          connection_id = ActiveRecord::Base.connection_id
          klasses = ActiveRecord::Base.turntable_connections.values
          klasses.each do |k|
            k.connection.enable_query_cache!
          end

          response = @app.call(env)
          response[2] = ::Rack::BodyProxy.new(response[2]) do
            restore_query_cache_settings(connection_id, enabled)
          end

          response
        rescue Exception => e
          restore_query_cache_settings(connection_id, enabled)
          raise e
        end

        private

        def restore_query_cache_settings(connection_id, enabled)
          klasses = ActiveRecord::Base.turntable_connections.values
          klasses.each do |k|
            ActiveRecord::Base.connection_id = connection_id
            k.connection.clear_query_cache
            k.connection.disable_query_cache! unless enabled
          end
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
activerecord-turntable-2.5.0 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.4.0 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.3.3 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.3.2 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.3.1 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.3.0 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.2.2 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.2.1 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.2.0 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.1.1 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.1.0 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.1.0.rc2 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.1.0.rc1 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.1.0.beta2 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.1.0.beta1 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.0.6 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.0.5 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.0.4 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.0.3 lib/active_record/turntable/rack/query_cache.rb
activerecord-turntable-2.0.2 lib/active_record/turntable/rack/query_cache.rb