Class: Github::Cache

Inherits:
Object
  • Object
show all
Includes:
Checks
Defined in:
app/github/cache.rb

Overview

A cache for GitHub API GET requests.

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Methods included from Checks

#check_boolean, #check_enumerable_of, #check_is_a, #check_non_empty_string, #check_non_nil, #check_positive_integer

Constructor Details

#initialize(hash, logger: Logger.new($stdout)) ⇒ Cache

Returns a new instance of Cache.



24
25
26
27
28
# File 'app/github/cache.rb', line 24

def initialize(hash, logger: Logger.new($stdout))
  @mutex = Mutex.new
  @hash = check_is_a(hash: [Hash, hash])
  @logger = check_non_nil(logger: logger)
end

Instance Method Details

#[](key) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/github/cache.rb', line 30

def [](key)
  @mutex.synchronize do
    evict
    if (value = @hash[key])
      @logger.info "#{self.class} entry found for #{key}"
      value
    else
      @logger.info "#{self.class} entry not found for #{key}"
      nil
    end
  end
end

#[]=(key, value) ⇒ Object



43
44
45
46
47
48
49
50
# File 'app/github/cache.rb', line 43

def []=(key, value)
  check_is_a(value: [Entry, value])

  @mutex.synchronize do
    @logger.info "#{self.class} caching entry for #{key} until #{value.expires_at}"
    @hash[key] = value
  end
end