Class: Ey::Core::ResponseCache
- Inherits:
-
Object
- Object
- Ey::Core::ResponseCache
- Defined in:
- lib/ey-core/response_cache.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}, &block) ⇒ ResponseCache
constructor
A new instance of ResponseCache.
Constructor Details
#initialize(app, options = {}, &block) ⇒ ResponseCache
Returns a new instance of ResponseCache.
2 3 4 5 6 |
# File 'lib/ey-core/response_cache.rb', line 2 def initialize(app, = {}, &block) @app = app @cache = .fetch(:cache, &block) @cache_key_prefix = .fetch(:cache_key_prefix, :ey_core) end |
Instance Method Details
#call(env) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ey-core/response_cache.rb', line 8 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 |