Class: Rack::Client::Cache::Context

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

Instance Method Summary collapse

Methods included from DualBand

#call

Methods included from Options

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

Constructor Details

#initialize(app, options = {}) ⇒ Context

Returns a new instance of Context.



8
9
10
11
12
# File 'lib/rack/client/middleware/cache/context.rb', line 8

def initialize(app, options = {})
  @app = app

  initialize_options options
end

Instance Method Details

#async_call(env) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rack/client/middleware/cache/context.rb', line 36

def async_call(env)
  @trace = []
  request = Request.new(options.merge(env))

  if request.cacheable?
    @app.call(env = request.env) do |response_parts|
      response = Response.new(*response_parts)

      if response.not_modified?
        response = lookup(request)
      elsif response.cacheable?
        store(request, response)
      else
        pass(env)
      end

      trace = @trace.join(', ')
      response.headers['X-Rack-Client-Cache'] = trace

      yield response.to_a
    end
  else
    @app.call(env) {|*response| yield *response }
  end
end

#entitystoreObject



83
84
85
86
# File 'lib/rack/client/middleware/cache/context.rb', line 83

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

#forward(request) ⇒ Object



98
99
100
# File 'lib/rack/client/middleware/cache/context.rb', line 98

def forward(request)
  Response.new(*@app.call(request.env))
end

#lookup(request) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/rack/client/middleware/cache/context.rb', line 62

def lookup(request)
  begin
    entry = metastore.lookup(request, entitystore)
    record :fresh
    entry
  rescue Exception => e
    log_error(e)
    return pass(request)
  end
end

#metastoreObject



78
79
80
81
# File 'lib/rack/client/middleware/cache/context.rb', line 78

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

#pass(request) ⇒ Object



93
94
95
96
# File 'lib/rack/client/middleware/cache/context.rb', line 93

def pass(request)
  record :pass
  forward(request)
end

#record(event) ⇒ Object

Record that an event took place.



89
90
91
# File 'lib/rack/client/middleware/cache/context.rb', line 89

def record(event)
  @trace << event
end

#store(request, response) ⇒ Object



73
74
75
76
# File 'lib/rack/client/middleware/cache/context.rb', line 73

def store(request, response)
  metastore.store(request, response, entitystore)
  record :store
end

#sync_call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rack/client/middleware/cache/context.rb', line 14

def sync_call(env)
  @trace = []
  request = Request.new(options.merge(env))

  return @app.call(env) unless request.cacheable?

  response = Response.new(*@app.call(env = request.env))

  if response.not_modified?
    response = lookup(request)
  elsif response.cacheable?
    store(request, response)
  else
    pass(request)
  end

  trace = @trace.join(', ')
  response.headers['X-Rack-Client-Cache'] = trace

  response.to_a
end