Class: Danger::HTTPCache

Inherits:
Object
  • Object
show all
Defined in:
lib/danger/commands/local_helpers/http_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_file = nil, options = {}) ⇒ HTTPCache

Returns a new instance of HTTPCache.



7
8
9
10
11
# File 'lib/danger/commands/local_helpers/http_cache.rb', line 7

def initialize(cache_file = nil, options = {})
  File.delete(cache_file) if options[:clear_cache]
  @store = PStore.new(cache_file)
  @expires_in = options[:expires_in] || 300 # 5 minutes
end

Instance Attribute Details

#expires_inObject (readonly)

Returns the value of attribute expires_in.



5
6
7
# File 'lib/danger/commands/local_helpers/http_cache.rb', line 5

def expires_in
  @expires_in
end

Instance Method Details

#delete(key) ⇒ Object



24
25
26
# File 'lib/danger/commands/local_helpers/http_cache.rb', line 24

def delete(key)
  @store.transaction { @store.delete key }
end

#entry_has_expired(entry, ttl) ⇒ Object



34
35
36
# File 'lib/danger/commands/local_helpers/http_cache.rb', line 34

def entry_has_expired(entry, ttl)
  Time.now.to_i > entry[:updated_at].to_i + ttl.to_i
end

#read(key) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/danger/commands/local_helpers/http_cache.rb', line 13

def read(key)
  @store.transaction do
    entry = @store[key]
    return nil unless entry
    return entry[:value] unless entry_has_expired(entry, @expires_in)

    @store.delete key
    return nil
  end
end

#write(key, value) ⇒ Object



28
29
30
31
32
# File 'lib/danger/commands/local_helpers/http_cache.rb', line 28

def write(key, value)
  @store.transaction do
    @store[key] = { updated_at: Time.now.to_i, value: value }
  end
end