class Cerner::OAuth1a::Cache::DefaultCache

Internal: The default implementation of the Cerner::OAuth1a::Cache interface. This implementation just maintains a capped list of entries in memory.

Public Class Methods

new(max:) click to toggle source

Internal: Constructs an instance.

Calls superclass method Cerner::OAuth1a::Cache::new
# File lib/cerner/oauth1a/cache.rb, line 70
def initialize(max:)
  super()
  @max = max
  @lock = Mutex.new
  @entries = {}
end

Public Instance Methods

get(namespace, key) click to toggle source

Internal: Gets an entry from the cache.

# File lib/cerner/oauth1a/cache.rb, line 88
def get(namespace, key)
  @lock.synchronize do
    prune_expired(Time.now.utc.to_i)
    @entries[full_key(namespace, key)]
  end
end
put(namespace, key, entry) click to toggle source

Internal: Puts an entry into the cache.

# File lib/cerner/oauth1a/cache.rb, line 78
def put(namespace, key, entry)
  @lock.synchronize do
    now = Time.now.utc.to_i
    prune_expired(now)
    @entries[full_key(namespace, key)] = entry
    prune_size
  end
end

Private Instance Methods

prune_expired(now) click to toggle source
# File lib/cerner/oauth1a/cache.rb, line 97
def prune_expired(now)
  return if @entries.empty?

  @entries.delete_if { |_, v| v.expired?(now) }

  nil
end
prune_size() click to toggle source
# File lib/cerner/oauth1a/cache.rb, line 105
def prune_size
  return if @entries.empty? || @entries.size <= @max

  num_to_prune = @entries.size - @max
  num_to_prune.times { @entries.shift }

  nil
end