Sha256: f493d31f3839eb621cad2b9dc74387f5b58c3cab91435eed40bdc6c5711011e1

Contents?: true

Size: 845 Bytes

Versions: 1

Compression:

Stored size: 845 Bytes

Contents

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

1 entries across 1 versions & 1 rubygems

Version Path
protobuf-activerecord-7.0.0 lib/protobuf/active_record/middleware/query_cache.rb