Class: Rack::Cache::Context

Inherits:
Object
  • Object
show all
Includes:
Options
Defined in:
lib/rack/cache/context.rb

Overview

Implements Rack’s middleware interface and provides the context for all cache logic, including the core logic engine.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Options

option_accessor, option_name, #options, #options=, #set

Constructor Details

#initialize(backend, options = {}) {|_self| ... } ⇒ Context

Returns a new instance of Context.

Yields:

  • (_self)

Yield Parameters:



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rack/cache/context.rb', line 18

def initialize(backend, options={})
  @backend = backend
  @trace = []
  @env = nil
  @options = options

  initialize_options options
  yield self if block_given?

  @private_header_keys =
    private_headers.map { |name| "HTTP_#{name.upcase.tr('-', '_')}" }
end

Instance Attribute Details

#backendObject (readonly)

The Rack application object immediately downstream.



16
17
18
# File 'lib/rack/cache/context.rb', line 16

def backend
  @backend
end

#traceObject (readonly)

Array of trace Symbols



13
14
15
# File 'lib/rack/cache/context.rb', line 13

def trace
  @trace
end

Instance Method Details

#call(env) ⇒ Object

The Rack call interface. The receiver acts as a prototype and runs each request in a dup object unless the rack.run_once variable is set in the environment.



48
49
50
51
52
53
54
# File 'lib/rack/cache/context.rb', line 48

def call(env)
  if env['rack.run_once'] && !env['rack.multithread']
    call! env
  else
    clone.call! env
  end
end

#call!(env) ⇒ Object

The real Rack call interface. The caching logic is performed within the context of the receiver.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rack/cache/context.rb', line 58

def call!(env)
  @trace = []
  @default_options.each { |k,v| env[k] ||= v }
  @env = env
  @request = Request.new(@env.dup.freeze)

  response =
    if @request.get? || @request.head?
      if !@env['HTTP_EXPECT'] && !@env['rack-cache.force-pass']
        lookup
      else
        pass
      end
    else
      if @request.options?
        pass
      else
        invalidate
      end
    end

  # log trace and set x-rack-cache tracing header
  trace = @trace.join(', ')
  response.headers['x-rack-cache'] = trace

  # write log message to rack.errors
  if verbose?
    message = "cache: [%s %s] %s\n" %
      [@request.request_method, @request.fullpath, trace]
    log_info(message)
  end

  # tidy up response a bit
  if (@request.get? || @request.head?) && not_modified?(response)
    response.not_modified!
  end

  if @request.head?
    response.body.close if response.body.respond_to?(:close)
    response.body = []
  end
  response.to_a
end

#entitystoreObject

The configured EntityStore instance. Changing the rack-cache.entitystore value effects the result of this method immediately.



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

def entitystore
  uri = options['rack-cache.entitystore']
  storage.resolve_entitystore_uri(uri, @options)
end

#metastoreObject

The configured MetaStore instance. Changing the rack-cache.metastore value effects the result of this method immediately.



33
34
35
36
# File 'lib/rack/cache/context.rb', line 33

def metastore
  uri = options['rack-cache.metastore']
  storage.resolve_metastore_uri(uri, @options)
end