Class: Lich::Gemstone::Infomon::Cache
- Inherits:
-
Object
- Object
- Lich::Gemstone::Infomon::Cache
- Defined in:
- documented/gemstone/infomon/cache.rb
Overview
Represents a cache for storing key-value pairs.
This class provides methods to put, get, delete, and check for keys in the cache.
Instance Attribute Summary collapse
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Instance Method Summary collapse
-
#delete(key) ⇒ Object?
Deletes the value associated with the given key from the cache.
-
#flush! ⇒ void
(also: #clear)
Clears all records from the cache.
-
#get(key) {|key| ... } ⇒ Object?
Retrieves the value associated with the given key from the cache.
-
#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 the given hash into the cache.
-
#put(key, value) ⇒ Cache
Stores a value in the cache associated with the given key.
-
#to_a ⇒ Array<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.
14 15 16 |
# File 'documented/gemstone/infomon/cache.rb', line 14 def initialize() @records = {} end |
Instance Attribute Details
#records ⇒ Object (readonly)
Returns the value of attribute records.
10 11 12 |
# File 'documented/gemstone/infomon/cache.rb', line 10 def records @records end |
Instance Method Details
#delete(key) ⇒ Object?
Deletes the value associated with the given key from the cache.
43 44 45 |
# File 'documented/gemstone/infomon/cache.rb', line 43 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.
36 37 38 |
# File 'documented/gemstone/infomon/cache.rb', line 36 def flush! @records.clear end |
#get(key) {|key| ... } ⇒ Object?
Retrieves the value associated with the given key from the cache. If the key does not exist, it yields to a block if given.
52 53 54 55 56 57 58 59 |
# File 'documented/gemstone/infomon/cache.rb', line 52 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.
30 31 32 |
# File 'documented/gemstone/infomon/cache.rb', line 30 def include?(key) @records.include?(key) end |
#merge!(h) ⇒ Hash
Merges the given hash into the cache.
64 65 66 |
# File 'documented/gemstone/infomon/cache.rb', line 64 def merge!(h) @records.merge!(h) end |
#put(key, value) ⇒ Cache
Stores a value in the cache associated with the given key.
22 23 24 25 |
# File 'documented/gemstone/infomon/cache.rb', line 22 def put(key, value) @records[key] = value self end |
#to_a ⇒ Array<Array>
Converts the cache records to an array of key-value pairs.
70 71 72 |
# File 'documented/gemstone/infomon/cache.rb', line 70 def to_a() @records.to_a end |
#to_h ⇒ Hash
Returns the cache records as a hash.
76 77 78 |
# File 'documented/gemstone/infomon/cache.rb', line 76 def to_h() @records end |