Sha256: 61acea29061fda0605bd65da9a7ea845bce8ca859a985e49150eb8642d1af64e

Contents?: true

Size: 1.86 KB

Versions: 17

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module Switchman
  module ActiveRecord
    module QueryCache
      private

      if ::Rails.version < "7.1"
        def cache_sql(sql, name, binds)
          # have to include the shard id in the cache key because of switching dbs on the same connection
          sql = "#{shard.id}::#{sql}"
          @lock.synchronize do
            result =
              if query_cache[sql].key?(binds)
                args = {
                  sql: sql,
                  binds: binds,
                  name: name,
                  connection_id: object_id,
                  cached: true,
                  type_casted_binds: -> { type_casted_binds(binds) }
                }
                ::ActiveSupport::Notifications.instrument(
                  "sql.active_record",
                  args
                )
                query_cache[sql][binds]
              else
                query_cache[sql][binds] = yield
              end
            result.dup
          end
        end
      else
        def cache_sql(sql, name, binds)
          # have to include the shard id in the cache key because of switching dbs on the same connection
          sql = "#{shard.id}::#{sql}"
          key = binds.empty? ? sql : [sql, binds]
          result = nil
          hit = false

          @lock.synchronize do
            if (result = @query_cache.delete(key))
              hit = true
              @query_cache[key] = result
            else
              result = @query_cache[key] = yield
              @query_cache.shift if @query_cache_max_size && @query_cache.size > @query_cache_max_size
            end
          end

          if hit
            ::ActiveSupport::Notifications.instrument(
              "sql.active_record",
              cache_notification_info(sql, name, binds)
            )
          end

          result.dup
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
switchman-3.6.7 lib/switchman/active_record/query_cache.rb
switchman-3.6.6 lib/switchman/active_record/query_cache.rb
switchman-3.6.5 lib/switchman/active_record/query_cache.rb
switchman-3.6.3 lib/switchman/active_record/query_cache.rb
switchman-3.6.2 lib/switchman/active_record/query_cache.rb
switchman-3.6.1 lib/switchman/active_record/query_cache.rb
switchman-3.6.0 lib/switchman/active_record/query_cache.rb
switchman-3.5.22 lib/switchman/active_record/query_cache.rb
switchman-3.5.21 lib/switchman/active_record/query_cache.rb
switchman-3.5.20 lib/switchman/active_record/query_cache.rb
switchman-3.5.19 lib/switchman/active_record/query_cache.rb
switchman-3.5.18 lib/switchman/active_record/query_cache.rb
switchman-3.5.17 lib/switchman/active_record/query_cache.rb
switchman-3.5.16 lib/switchman/active_record/query_cache.rb
switchman-3.5.15 lib/switchman/active_record/query_cache.rb
switchman-3.5.14 lib/switchman/active_record/query_cache.rb
switchman-3.5.13 lib/switchman/active_record/query_cache.rb