Class: JIRA::RequestCache

Inherits:
Object
  • Object
show all
Defined in:
lib/jira/request_cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(time_to_live) ⇒ RequestCache

Returns a new instance of RequestCache.



5
6
7
8
# File 'lib/jira/request_cache.rb', line 5

def initialize(time_to_live)
  @time_to_live = time_to_live
  @hash_creator = Digest::MD5.new
end

Instance Method Details

#load(uri) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jira/request_cache.rb', line 10

def load(uri)
  key = cache_key(uri)
  cache_object = cache(uri)
  response = verify cache_object[key]

  if response == :expired
    cache_object.delete(key)
    cache_file(uri, 'w+').write(Marshal.dump(cache_object))
    nil
  else
    response
  end
end

#save(uri, response) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jira/request_cache.rb', line 24

def save(uri, response)
  now = Time.now.to_i

  new_cache = cache(uri)
  new_cache[cache_key(uri)] = {
    'data' => response,
    'timestamp' => now
  }

  cache_file(uri, 'w+b').write(Marshal.dump(new_cache))
end