Class: Lich::Gemstone::Infomon::Cache
- Inherits:
-
Object
- Object
- Lich::Gemstone::Infomon::Cache
- Defined in:
- lib/gemstone/infomon/cache.rb
Overview
in-memory cache with db read fallbacks
This class provides a simple in-memory cache that allows for storing, retrieving, and managing key-value pairs. It also supports fallback mechanisms for retrieving values when they are not present in the cache.
Instance Attribute Summary collapse
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Instance Method Summary collapse
-
#delete(key) ⇒ Object?
Deletes a key-value pair from the cache.
-
#flush! ⇒ void
(also: #clear)
Clears all records from the cache.
-
#get(key) {|key| ... } ⇒ Object?
Retrieves a value from the cache, or computes it using a block if not present.
-
#include?(key) ⇒ Boolean
(also: #key?)
Checks if a key exists in the cache.
-
#initialize ⇒ Cache
constructor
Initializes a new Cache instance.
-
#merge!(h) ⇒ Hash
Merges another hash into the cache.
-
#put(key, value) ⇒ Cache
Stores a value in the cache associated with a given key.
-
#to_a ⇒ Array
Converts the cache records to an array of key-value pairs.
-
#to_h ⇒ Hash
Converts the cache records to a hash.
Constructor Details
#initialize ⇒ Cache
Initializes a new Cache instance.
15 16 17 |
# File 'lib/gemstone/infomon/cache.rb', line 15 def initialize() @records = {} end |
Instance Attribute Details
#records ⇒ Object (readonly)
Returns the value of attribute records.
10 11 12 |
# File 'lib/gemstone/infomon/cache.rb', line 10 def records @records end |
Instance Method Details
#delete(key) ⇒ Object?
Deletes a key-value pair from the cache.
67 68 69 |
# File 'lib/gemstone/infomon/cache.rb', line 67 def delete(key) @records.delete(key) end |
#flush! ⇒ void Also known as: clear
This method returns an undefined value.
Clears all records from the cache.
54 55 56 |
# File 'lib/gemstone/infomon/cache.rb', line 54 def flush! @records.clear end |
#get(key) {|key| ... } ⇒ Object?
Retrieves a value from the cache, or computes it using a block if not present.
81 82 83 84 85 86 87 88 |
# File 'lib/gemstone/infomon/cache.rb', line 81 def get(key) return @records[key] if self.include?(key) miss = nil miss = yield(key) if block_given? # don't cache nils return miss if miss.nil? @records[key] = miss end |
#include?(key) ⇒ Boolean Also known as: key?
Checks if a key exists in the cache.
42 43 44 |
# File 'lib/gemstone/infomon/cache.rb', line 42 def include?(key) @records.include?(key) end |
#merge!(h) ⇒ Hash
Merges another hash into the cache.
98 99 100 |
# File 'lib/gemstone/infomon/cache.rb', line 98 def merge!(h) @records.merge!(h) end |
#put(key, value) ⇒ Cache
Stores a value in the cache associated with a given key.
28 29 30 31 |
# File 'lib/gemstone/infomon/cache.rb', line 28 def put(key, value) @records[key] = value self end |
#to_a ⇒ Array
Converts the cache records to an array of key-value pairs.
110 111 112 |
# File 'lib/gemstone/infomon/cache.rb', line 110 def to_a() @records.to_a end |
#to_h ⇒ Hash
Converts the cache records to a hash.
122 123 124 |
# File 'lib/gemstone/infomon/cache.rb', line 122 def to_h() @records end |