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.



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

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



22
23
24
# File 'lib/danger/commands/local_helpers/http_cache.rb', line 22

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

#entry_has_expired(entry, ttl) ⇒ Object



32
33
34
# File 'lib/danger/commands/local_helpers/http_cache.rb', line 32

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

#read(key) ⇒ Object



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

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



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

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