Class: Heartcheck::CachingApp

Inherits:
Object
  • Object
show all
Defined in:
lib/heartcheck/caching_app.rb,
lib/heartcheck/caching_app/cache.rb

Defined Under Namespace

Classes: Cache

Instance Method Summary collapse

Constructor Details

#initialize(app, ttl = 300, cache = nil) ⇒ #call

Creates an instance of the middleware

Parameters:

  • app (Heartcheck:App)

    the Rack app to wrap around

  • ttl (Integer) (defaults to: 300)

    the time to cache the results in seconds

  • cache (Heartcheck::CachingApp::Cache) (defaults to: nil)

    the cache instance to use The cache will be created on first use if not supplied



16
17
18
19
20
# File 'lib/heartcheck/caching_app.rb', line 16

def initialize(app, ttl = 300, cache = nil)
  @app = app
  @ttl = ttl
  @cache = cache
end

Instance Method Details

#call(env) ⇒ Array

Invokes the middleware

Parameters:

  • env (Hash)

    the rack request/environment

Returns:

  • (Array)

    a rack compatible response



26
27
28
29
30
31
32
33
34
35
# File 'lib/heartcheck/caching_app.rb', line 26

def call(env)
  req = Rack::Request.new(env)
  controller = Heartcheck::App::ROUTE_TO_CONTROLLER[req.path_info]

  if controller && (result = cache.result(controller))
    [200, { 'Content-type' => 'application/json' }, [result]]
  else
    @app.call(env)
  end
end