Module: Rack::Cache::Core

Included in:
Context
Defined in:
lib/rack/cache/core.rb

Overview

The core logic engine and state machine. When a request is received, the engine begins transitioning from state to state based on the advice given by events. Each transition performs some piece of core logic, calls out to an event handler, and then kicks off the next transition.

Five objects of interest are made available during execution:

  • original_request - The request as originally received. This object is never modified.

  • request - The request that may eventually be sent downstream in case of pass or miss. This object defaults to the original_request but may be modified or replaced entirely.

  • original_response - The response exactly as specified by the downstream application; nil on cache hit.

  • entry - The response loaded from cache or stored to cache. This object becomes response if the cached response is valid.

  • response - The response that will be delivered upstream after processing is complete. This object may be modified as necessary.

These objects can be accessed and modified from within event handlers to perform various types of request/response manipulation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#entryObject (readonly)

A response object retrieved from cache, or the response that is to be saved to cache, or nil if no cached response was found. The object is an instance of the Rack::Cache::Response class.



54
55
56
# File 'lib/rack/cache/core.rb', line 54

def entry
  @entry
end

#original_requestObject (readonly)

The request exactly as received. The object is an instance of the Rack::Cache::Request class, which includes many utility methods for inspecting the state of the request.

This object cannot be modified. If the request requires modification before being delivered to the downstream application, use the #request object.



41
42
43
# File 'lib/rack/cache/core.rb', line 41

def original_request
  @original_request
end

#original_responseObject (readonly)

The response exactly as received from the downstream application. The object is an instance of the Rack::Cache::Response class, which includes utility methods for inspecting the state of the response.

The original response should not be modified. Use the #response object to access the response to be sent back upstream.



49
50
51
# File 'lib/rack/cache/core.rb', line 49

def original_response
  @original_response
end

#requestObject (readonly)

The request that will be made downstream on the application. This defaults to the request exactly as received (#original_request). The object is an instance of the Rack::Cache::Request class, which includes utility methods for inspecting and modifying various aspects of the HTTP request.



61
62
63
# File 'lib/rack/cache/core.rb', line 61

def request
  @request
end

#responseObject (readonly)

The response that will be sent upstream. Defaults to the response received from the downstream application (#original_response) but is set to the cached #entry when valid. In any case, the object is an instance of the Rack::Cache::Response class, which includes a variety of utility methods for inspecting and modifying the HTTP response.



69
70
71
# File 'lib/rack/cache/core.rb', line 69

def response
  @response
end

Instance Method Details

#on(*events, &block) ⇒ Object

Attach custom logic to one or more events.



83
84
85
86
# File 'lib/rack/cache/core.rb', line 83

def on(*events, &block)
  events.each { |event| @events[event].unshift(block) }
  nil
end

#performed?(event) ⇒ Boolean

Has the given event been performed at any time during the request life-cycle? Useful for testing.

Returns:

  • (Boolean)


73
74
75
# File 'lib/rack/cache/core.rb', line 73

def performed?(event)
  @triggered.include?(event)
end