Sha256: e84a8309b12b16cb76b673fa4b0ea52bf5577ccf4246e590a2e0d1a539f98a41

Contents?: true

Size: 861 Bytes

Versions: 21

Compression:

Stored size: 861 Bytes

Contents

require "thread"

module Protobuf
  module ActiveRecord
    module Middleware
      class QueryCache
        CURRENT_CONNECTION = "_protobuf_active_record_current_connection".freeze

        def initialize(app)
          @app = app
        end

        def call(env)
          connection = ::Thread.current[CURRENT_CONNECTION] = ::ActiveRecord::Base.connection
          enabled = connection.query_cache_enabled
          connection.enable_query_cache!

          @app.call(env)
        ensure
          restore_query_cache_settings(enabled)
        end

      private

        def restore_query_cache_settings(enabled)
          ::Thread.current[CURRENT_CONNECTION].clear_query_cache
          ::Thread.current[CURRENT_CONNECTION].disable_query_cache! unless enabled
          ::Thread.current[CURRENT_CONNECTION] = nil
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
protobuf-activerecord-6.1.0 lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-6.0.0 lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-5.2.0 lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-5.1.0 lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-5.0.0 lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-5.1.0.beta lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-5.0.0.beta lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-3.6.1 lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-3.6.0 lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-3.5.1 lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-3.5.0 lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-3.4.4 lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-3.4.4.pre lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-3.4.3 lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-3.4.2 lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-3.4.1 lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-3.4.0 lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-3.3.8 lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-3.3.7 lib/protobuf/active_record/middleware/query_cache.rb
protobuf-activerecord-3.3.6 lib/protobuf/active_record/middleware/query_cache.rb