Sha256: 2cbc633ca681275301e7c362ebff8d39076dca16f9e3025637c80dcb8af292a6

Contents?: true

Size: 1.19 KB

Versions: 226

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

require "rack/body_proxy"
require "rack/utils"

module ActiveSupport
  module Cache
    module Strategy
      module LocalCache
        #--
        # This class wraps up local storage for middlewares. Only the middleware method should
        # construct them.
        class Middleware # :nodoc:
          attr_reader :name, :local_cache_key

          def initialize(name, local_cache_key)
            @name = name
            @local_cache_key = local_cache_key
            @app = nil
          end

          def new(app)
            @app = app
            self
          end

          def call(env)
            LocalCacheRegistry.set_cache_for(local_cache_key, LocalStore.new)
            response = @app.call(env)
            response[2] = ::Rack::BodyProxy.new(response[2]) do
              LocalCacheRegistry.set_cache_for(local_cache_key, nil)
            end
            cleanup_on_body_close = true
            response
          rescue Rack::Utils::InvalidParameterError
            [400, {}, []]
          ensure
            LocalCacheRegistry.set_cache_for(local_cache_key, nil) unless
              cleanup_on_body_close
          end
        end
      end
    end
  end
end

Version data entries

226 entries across 211 versions & 26 rubygems

Version Path
activesupport-8.0.0 lib/active_support/cache/strategy/local_cache_middleware.rb
activesupport-7.2.2 lib/active_support/cache/strategy/local_cache_middleware.rb
activesupport-7.1.5 lib/active_support/cache/strategy/local_cache_middleware.rb
activesupport-8.0.0.rc2 lib/active_support/cache/strategy/local_cache_middleware.rb
activesupport-7.2.1.2 lib/active_support/cache/strategy/local_cache_middleware.rb
activesupport-7.1.4.2 lib/active_support/cache/strategy/local_cache_middleware.rb
activesupport-7.0.8.6 lib/active_support/cache/strategy/local_cache_middleware.rb
activesupport-6.1.7.10 lib/active_support/cache/strategy/local_cache_middleware.rb
activesupport-8.0.0.rc1 lib/active_support/cache/strategy/local_cache_middleware.rb
activesupport-6.1.7.9 lib/active_support/cache/strategy/local_cache_middleware.rb
activesupport-7.2.1.1 lib/active_support/cache/strategy/local_cache_middleware.rb
activesupport-7.1.4.1 lib/active_support/cache/strategy/local_cache_middleware.rb
activesupport-7.0.8.5 lib/active_support/cache/strategy/local_cache_middleware.rb
activesupport-8.0.0.beta1 lib/active_support/cache/strategy/local_cache_middleware.rb
omg-activesupport-8.0.0.alpha9 lib/active_support/cache/strategy/local_cache_middleware.rb
omg-activesupport-8.0.0.alpha8 lib/active_support/cache/strategy/local_cache_middleware.rb
omg-activesupport-8.0.0.alpha7 lib/active_support/cache/strategy/local_cache_middleware.rb
omg-activesupport-8.0.0.alpha4 lib/active_support/cache/strategy/local_cache_middleware.rb
omg-activesupport-8.0.0.alpha3 lib/active_support/cache/strategy/local_cache_middleware.rb
omg-activesupport-8.0.0.alpha2 lib/active_support/cache/strategy/local_cache_middleware.rb