class Warden::GitHub::MembershipCache

A hash subclass that acts as a cache for organization and team membership states. Only membership states that are true are cached. These are invalidated after a certain time.

Constants

CACHE_TIMEOUT

Public Class Methods

new(data) click to toggle source
# File lib/warden/github/membership_cache.rb, line 9
def initialize(data)
  @data = data
end

Public Instance Methods

fetch_membership(type, id) { || ... } click to toggle source

Fetches a membership status by type and id (e.g. 'org', 'my_company') from cache. If no cached value is present or if the cached value expired, the block will be invoked and the return value, if true, cached for e certain time.

# File lib/warden/github/membership_cache.rb, line 17
def fetch_membership(type, id)
  type = type.to_s
  id = id.to_s

  if cached_membership_valid?(type, id)
    true
  elsif block_given? && yield
    cache_membership(type, id)
    true
  else
    false
  end
end

Private Instance Methods

cache_membership(type, id) click to toggle source
# File lib/warden/github/membership_cache.rb, line 46
def cache_membership(type, id)
  hash = @data[type] ||= {}
  hash[id] = Time.now.to_i
end
cached_membership_valid?(type, id) click to toggle source
# File lib/warden/github/membership_cache.rb, line 33
def cached_membership_valid?(type, id)
  timestamp = @data.fetch(type).fetch(id)

  if Time.now.to_i > timestamp + CACHE_TIMEOUT
    @data.fetch(type).delete(id)
    false
  else
    true
  end
rescue IndexError
  false
end