Class: Cardiac::ResourceCache::Middleware

Inherits:
Object
  • Object
show all
Includes:
InstanceMethods
Defined in:
lib/cardiac/resource/cache.rb

Overview

Simple middleware for Rack, enabling a resource cache for the duration of the block.

Most of this has been “borrowed” from ActiveRecord.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



94
95
96
# File 'lib/cardiac/resource/cache.rb', line 94

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cardiac/resource/cache.rb', line 98

def call(env)
  enabled = resource_cache_enabled?
  enable_resource_cache!
  
  response = @app.call(env)
  response[2] = Rack::BodyProxy.new(response[2]) do
    restore_resource_cache_settings(enabled)
  end
  
  response
rescue Exception => e
  restore_resource_cache_settings(enabled)
  raise e
end