Class: Sinew::Cache

Inherits:
Object show all
Defined in:
lib/sinew/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sinew) ⇒ Cache

Returns a new instance of Cache.



12
13
14
# File 'lib/sinew/cache.rb', line 12

def initialize(sinew)
  @sinew = sinew
end

Instance Attribute Details

#sinewObject (readonly)

Returns the value of attribute sinew.



10
11
12
# File 'lib/sinew/cache.rb', line 10

def sinew
  @sinew
end

Instance Method Details

#get(request) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/sinew/cache.rb', line 16

def get(request)
  body = read_if_exist(body_path(request))
  return nil if !body

  head = read_if_exist(head_path(request))
  Response.from_cache(request, body, head)
end

#set(response) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sinew/cache.rb', line 24

def set(response)
  body_path = body_path(response.request)
  head_path = head_path(response.request)

  FileUtils.mkdir_p(File.dirname(body_path))
  FileUtils.mkdir_p(File.dirname(head_path))

  # write body, and head if necessary
  atomic_write(body_path, response.body)
  if head_necessary?(response)
    head = JSON.pretty_generate(response.head_as_json)
    atomic_write(head_path, head)
  end
end