Sha256: 4584c3cfae65320f9d14955c0c5192dd24aad2e21340d2b8fb56e74e5469c849

Contents?: true

Size: 758 Bytes

Versions: 54

Compression:

Stored size: 758 Bytes

Contents

class Ey::Core::ResponseCache
  def initialize(app, options = {}, &block)
    @app = app
    @cache = options.fetch(:cache, &block)
    @cache_key_prefix = options.fetch(:cache_key_prefix, :ey_core)
  end

  def call(env)
    # Only cache "safe" requests
    return @app.call(env) unless [:get, :head].include?(env[:method])

    cache_key = [ @cache_key_prefix, env[:url].to_s ]
    cached = @cache.read(cache_key)

    if cached
      env[:request_headers]["If-None-Match"] ||= cached[:response_headers]["Etag"]
    end

    @app.call(env).on_complete do
      if cached && env[:status] == 304
        env[:body] = cached[:body]
      end

      if !cached && env[:response_headers]["Etag"]
        @cache.write(cache_key, env)
      end
    end
  end
end

Version data entries

54 entries across 54 versions & 3 rubygems

Version Path
ey-core-3.6.0.autoscaling1 lib/ey-core/response_cache.rb
ey-core-3.6.4 lib/ey-core/response_cache.rb
ey-core-3.6.3 lib/ey-core/response_cache.rb
ey-core-3.6.1 lib/ey-core/response_cache.rb
groove-ey-core-3.6.3 lib/ey-core/response_cache.rb
groove-ey-core-3.6.2 lib/ey-core/response_cache.rb
groove-ey-core-3.6.1 lib/ey-core/response_cache.rb
ey-core-3.5.0 lib/ey-core/response_cache.rb
ey-core-3.4.4 lib/ey-core/response_cache.rb
ey-core-3.4.2 lib/ey-core/response_cache.rb
ey-core-3.4.1 lib/ey-core/response_cache.rb
ey-core-3.4.0 lib/ey-core/response_cache.rb
ey-core-3.3.1 lib/ey-core/response_cache.rb
ey-core-3.3.0 lib/ey-core/response_cache.rb
ey-core-3.2.6 lib/ey-core/response_cache.rb
ey-core-3.2.5 lib/ey-core/response_cache.rb
ey-core-3.2.4 lib/ey-core/response_cache.rb
ey-core-3.2.3 lib/ey-core/response_cache.rb
ey-core-3.2.2 lib/ey-core/response_cache.rb
ey-core-3.2.1 lib/ey-core/response_cache.rb