Class: ADAL::MemoryCache
- Inherits:
-
Object
- Object
- ADAL::MemoryCache
- Includes:
- Logging
- Defined in:
- lib/adal/memory_cache.rb
Overview
A simple cache implementation that is not persisted across application runs.
Constant Summary
Constants included from Logging
Logging::DEFAULT_LOG_LEVEL, Logging::DEFAULT_LOG_OUTPUT
Instance Attribute Summary collapse
-
#entries ⇒ Object
Returns the value of attribute entries.
Class Method Summary collapse
-
.from_json(json) ⇒ Object
Reconstructs the cache from JSON that was previously serialized.
Instance Method Summary collapse
-
#add(entries) ⇒ Object
Adds an array of objects to the cache.
-
#find(&query) ⇒ Object
By default, matches all entries.
-
#initialize ⇒ MemoryCache
constructor
A new instance of MemoryCache.
-
#remove(entries) ⇒ Object
Removes an array of objects from the cache.
-
#to_json(_ = nil) ⇒ Object
Converts the cache entries into one JSON string.
Methods included from Logging
Constructor Details
#initialize ⇒ MemoryCache
30 31 32 |
# File 'lib/adal/memory_cache.rb', line 30 def initialize @entries = [] end |
Instance Attribute Details
#entries ⇒ Object
Returns the value of attribute entries.
34 35 36 |
# File 'lib/adal/memory_cache.rb', line 34 def entries @entries end |
Class Method Details
.from_json(json) ⇒ Object
Reconstructs the cache from JSON that was previously serialized.
87 88 89 90 91 92 93 |
# 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 |
Instance Method Details
#add(entries) ⇒ Object
Adds an array of objects to the cache.
43 44 45 46 47 48 |
# 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) ⇒ Object
By default, matches all entries.
57 58 59 60 |
# File 'lib/adal/memory_cache.rb', line 57 def find(&query) query ||= proc { true } @entries.select(&query) end |
#remove(entries) ⇒ Object
Removes an array of objects from the cache.
69 70 71 |
# File 'lib/adal/memory_cache.rb', line 69 def remove(entries) @entries -= Array(entries) end |
#to_json(_ = nil) ⇒ Object
Converts the cache entries into one JSON string.
78 79 80 |
# File 'lib/adal/memory_cache.rb', line 78 def to_json(_ = nil) JSON.unparse(entries) end |