Class: Lich::Gemstone::Infomon::Cache
- Inherits:
-
Object
- Object
- Lich::Gemstone::Infomon::Cache
- Defined in:
- documented/gemstone/infomon/cache.rb
Overview
in-memory cache with db read fallbacks In-memory cache with database read fallbacks This class provides a simple in-memory cache that can store key-value pairs. If a key is not found in the cache, a block can be provided to fetch the value from a database or other source.
Instance Attribute Summary collapse
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Instance Method Summary collapse
-
#delete(key) ⇒ Object?
Deletes a value from the cache by its key.
-
#flush! ⇒ void
(also: #clear)
Clears all records from the cache.
-
#get(key) ⇒ Object?
Retrieves a value from the cache by its key If the key is not found, it can yield to a block to fetch the value.
-
#include?(key) ⇒ Boolean
(also: #key?)
Checks if the cache includes the given key.
-
#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 with the given key.
-
#to_a ⇒ Array
Converts the cache records to an array of key-value pairs.
-
#to_h ⇒ Hash
Returns the cache records as a hash.
Constructor Details
#initialize ⇒ Cache
Initializes a new Cache instance
16 17 18 |
# File 'documented/gemstone/infomon/cache.rb', line 16 def initialize() @records = {} end |
Instance Attribute Details
#records ⇒ Object (readonly)
Returns the value of attribute records.
12 13 14 |
# File 'documented/gemstone/infomon/cache.rb', line 12 def records @records end |
Instance Method Details
#delete(key) ⇒ Object?
Deletes a value from the cache by its key
53 54 55 |
# File 'documented/gemstone/infomon/cache.rb', line 53 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
44 45 46 |
# File 'documented/gemstone/infomon/cache.rb', line 44 def flush! @records.clear end |
#get(key) ⇒ Object?
Retrieves a value from the cache by its key If the key is not found, it can yield to a block to fetch the value.
63 64 65 66 67 68 69 70 |
# File 'documented/gemstone/infomon/cache.rb', line 63 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 the cache includes the given key
36 37 38 |
# File 'documented/gemstone/infomon/cache.rb', line 36 def include?(key) @records.include?(key) end |
#merge!(h) ⇒ Hash
Merges another hash into the cache
77 78 79 |
# File 'documented/gemstone/infomon/cache.rb', line 77 def merge!(h) @records.merge!(h) end |
#put(key, value) ⇒ Cache
Stores a value in the cache with the given key
26 27 28 29 |
# File 'documented/gemstone/infomon/cache.rb', line 26 def put(key, value) @records[key] = value self end |
#to_a ⇒ Array
Converts the cache records to an array of key-value pairs
85 86 87 |
# File 'documented/gemstone/infomon/cache.rb', line 85 def to_a() @records.to_a end |
#to_h ⇒ Hash
Returns the cache records as a hash
93 94 95 |
# File 'documented/gemstone/infomon/cache.rb', line 93 def to_h() @records end |