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.

Methods

call   call!   entitystore   metastore   new  

Included Modules

Rack::Cache::Options

Attributes

backend  [R]  The Rack application object immediately downstream.
trace  [R]  Array of trace Symbols

Public Class methods

[Source]

    # File lib/rack/cache/context.rb, line 18
18:     def initialize(backend, options={})
19:       @backend = backend
20:       @trace = []
21: 
22:       initialize_options options
23:       yield self if block_given?
24: 
25:       @private_header_keys =
26:         private_headers.map { |name| "HTTP_#{name.upcase.tr('-', '_')}" }
27:     end

Public Instance methods

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.

[Source]

    # File lib/rack/cache/context.rb, line 46
46:     def call(env)
47:       if env['rack.run_once']
48:         call! env
49:       else
50:         clone.call! env
51:       end
52:     end

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

[Source]

    # File lib/rack/cache/context.rb, line 56
56:     def call!(env)
57:       @trace = []
58:       @default_options.each { |k,v| env[k] ||= v }
59:       @env = env
60:       @request = Request.new(@env.dup.freeze)
61: 
62:       response =
63:         if @request.get? || @request.head?
64:           if !@env['HTTP_EXPECT'] && !@env['rack-cache.force-pass']
65:             lookup
66:           else
67:             pass
68:           end
69:         else
70:           invalidate
71:         end
72: 
73:       # log trace and set X-Rack-Cache tracing header
74:       trace = @trace.join(', ')
75:       response.headers['X-Rack-Cache'] = trace
76: 
77:       # write log message to rack.errors
78:       if verbose?
79:         message = "cache: [%s %s] %s\n" %
80:           [@request.request_method, @request.fullpath, trace]
81:         @env['rack.errors'].write(message)
82:       end
83: 
84:       # tidy up response a bit
85:       response.not_modified! if not_modified?(response)
86:       if @request.head?
87:         response.body.close if response.body.respond_to?(:close)
88:         response.body = []
89:       end
90:       response.to_a
91:     end

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

[Source]

    # File lib/rack/cache/context.rb, line 38
38:     def entitystore
39:       uri = options['rack-cache.entitystore']
40:       storage.resolve_entitystore_uri(uri)
41:     end

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

[Source]

    # File lib/rack/cache/context.rb, line 31
31:     def metastore
32:       uri = options['rack-cache.metastore']
33:       storage.resolve_metastore_uri(uri)
34:     end

[Validate]