| Class | Rack::Cache::Context |
| In: |
lib/rack/cache/context.rb
|
| Parent: | Object |
Implements Rack‘s middleware interface and provides the context for all cache logic, including the core logic engine.
| backend | [R] | The Rack application object immediately downstream. |
| trace | [R] | Array of trace Symbols |
# File lib/rack/cache/context.rb, line 18
18: def initialize(backend, options={})
19: @backend = backend
20: @trace = []
21: @env = nil
22:
23: initialize_options options
24: yield self if block_given?
25:
26: @private_header_keys =
27: private_headers.map { |name| "HTTP_#{name.upcase.tr('-', '_')}" }
28: end
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.
# File lib/rack/cache/context.rb, line 47
47: def call(env)
48: if env['rack.run_once']
49: call! env
50: else
51: clone.call! env
52: end
53: end
The real Rack call interface. The caching logic is performed within the context of the receiver.
# File lib/rack/cache/context.rb, line 57
57: def call!(env)
58: @trace = []
59: @default_options.each { |k,v| env[k] ||= v }
60: @env = env
61: @request = Request.new(@env.dup.freeze)
62:
63: response =
64: if @request.get? || @request.head?
65: if !@env['HTTP_EXPECT'] && !@env['rack-cache.force-pass']
66: lookup
67: else
68: pass
69: end
70: else
71: invalidate
72: end
73:
74: # log trace and set X-Rack-Cache tracing header
75: trace = @trace.join(', ')
76: response.headers['X-Rack-Cache'] = trace
77:
78: # write log message to rack.errors
79: if verbose?
80: message = "cache: [%s %s] %s\n" %
81: [@request.request_method, @request.fullpath, trace]
82: @env['rack.errors'].write(message)
83: end
84:
85: # tidy up response a bit
86: if (@request.get? || @request.head?) && not_modified?(response)
87: response.not_modified!
88: end
89:
90: if @request.head?
91: response.body.close if response.body.respond_to?(:close)
92: response.body = []
93: end
94: response.to_a
95: end
The configured EntityStore instance. Changing the rack-cache.entitystore value effects the result of this method immediately.
# File lib/rack/cache/context.rb, line 39
39: def entitystore
40: uri = options['rack-cache.entitystore']
41: storage.resolve_entitystore_uri(uri)
42: end