class ADAL::MemoryCache

A simple cache implementation that is not persisted across application runs.

Attributes

entries[RW]

Public Class Methods

from_json(json) click to toggle source

Reconstructs the cache from JSON that was previously serialized.

@param JSON json @return MemoryCache

# File lib/adal/memory_cache.rb, line 87
def self.from_json(json)
  cache = MemoryCache.new
  cache.entries = JSON.parse(json).map do |e|
    CachedTokenResponse.from_json(e)
  end
  cache
end
new() click to toggle source
# File lib/adal/memory_cache.rb, line 30
def initialize
  @entries = []
end

Public Instance Methods

add(entries) click to toggle source

Adds an array of objects to the cache.

@param Array

The entries to add.

@return Array

The entries after the addition.
# File lib/adal/memory_cache.rb, line 43
def add(entries)
  entries = Array(entries)  # If entries is an array, this is a no-op.
  old_size = @entries.size
  @entries |= entries
  logger.verbose("Added #{entries.size - old_size} new entries to cache.")
end
find(&query) click to toggle source

By default, matches all entries.

@param Block

A matcher on the token list.

@return Array

The matching tokens.
# File lib/adal/memory_cache.rb, line 57
def find(&query)
  query ||= proc { true }
  @entries.select(&query)
end
remove(entries) click to toggle source

Removes an array of objects from the cache.

@param Array

The entries to remove.

@return Array

The remaining entries.
# File lib/adal/memory_cache.rb, line 69
def remove(entries)
  @entries -= Array(entries)
end
to_json(_ = nil) click to toggle source

Converts the cache entries into one JSON string.

@param JSON::Ext::Generator::State @return String

# File lib/adal/memory_cache.rb, line 78
def to_json(_ = nil)
  JSON.unparse(entries)
end