Sha256: b5e0ba17bc9699f3fa08c9a79f2336f06f1884180466ee669eced9eeac137217

Contents?: true

Size: 1.69 KB

Versions: 20

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module Switchman
  module ActiveSupport
    module Cache
      module ClassMethods
        def lookup_stores(cache_store_config)
          result = {}
          cache_store_config.each do |key, value|
            next if value.is_a?(String)

            result[key] = ::ActiveSupport::Cache.lookup_store(value)
          end

          cache_store_config.each do |key, value| # rubocop:disable Style/CombinableLoops
            next unless value.is_a?(String)

            result[key] = result[value]
          end
          result
        end

        def lookup_store(*store_options)
          store = super
          # can't use defined?, because it's a _ruby_ autoloaded constant,
          # so just checking that will cause it to get required
          ::ActiveSupport::Cache::RedisCacheStore.prepend(RedisCacheStore) if store.instance_of?(ActiveSupport::Cache::RedisCacheStore) && !::ActiveSupport::Cache::RedisCacheStore.ancestors.include?(RedisCacheStore)
          store.options[:namespace] ||= -> { Shard.current.default? ? nil : "shard_#{Shard.current.id}" }
          store
        end
      end

      module RedisCacheStore
        def clear(namespace: nil, **)
          # RedisCacheStore tries to be smart and only clear the cache under your namespace, if you have one set
          # unfortunately, it uses the keys command, which is extraordinarily inefficient in a large redis instance
          # fortunately, we can assume we control the entire instance, because we set up the namespacing, so just
          # always unset it temporarily for clear calls
          namespace = nil # rubocop:disable Lint/ShadowedArgument
          super
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
switchman-3.4.2 lib/switchman/active_support/cache.rb
switchman-3.4.1 lib/switchman/active_support/cache.rb
switchman-3.4.0 lib/switchman/active_support/cache.rb
switchman-3.3.7 lib/switchman/active_support/cache.rb
switchman-3.3.6 lib/switchman/active_support/cache.rb
switchman-3.3.5 lib/switchman/active_support/cache.rb
switchman-3.3.4 lib/switchman/active_support/cache.rb
switchman-3.3.3 lib/switchman/active_support/cache.rb
switchman-3.3.2 lib/switchman/active_support/cache.rb
switchman-3.3.1 lib/switchman/active_support/cache.rb
switchman-3.3.0 lib/switchman/active_support/cache.rb
switchman-3.2.1 lib/switchman/active_support/cache.rb
switchman-3.2.0 lib/switchman/active_support/cache.rb
switchman-3.1.3 lib/switchman/active_support/cache.rb
switchman-3.1.2 lib/switchman/active_support/cache.rb
switchman-3.1.1 lib/switchman/active_support/cache.rb
switchman-3.1.0 lib/switchman/active_support/cache.rb
switchman-3.0.24 lib/switchman/active_support/cache.rb
switchman-3.0.23 lib/switchman/active_support/cache.rb
switchman-3.0.22 lib/switchman/active_support/cache.rb