Class: Lich::Gemstone::Infomon::Cache

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeCache

Initializes a new Cache instance.



15
16
17
# File 'lib/gemstone/infomon/cache.rb', line 15

def initialize()
  @records = {}
end

Instance Attribute Details

#recordsObject (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.

Examples:

cache = Cache.new
cache.put(:foo, 'bar')
cache.delete(:foo) # => 'bar'

Parameters:

  • key (Object)

    the key to delete from the cache.

Returns:

  • (Object, nil)

    the value associated with the key, or nil if the key was not found.



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.

Examples:

cache = Cache.new
cache.put(:foo, 'bar')
cache.flush! # clears 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.

Examples:

cache = Cache.new
value = cache.get(:foo) { 'default' } # => 'default'

Parameters:

  • key (Object)

    the key to retrieve from the cache.

Yields:

  • (key)

    a block to compute the value if it is not present in the cache.

Returns:

  • (Object, nil)

    the value associated with the key, or nil if not found and block returns nil.



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.

Examples:

cache = Cache.new
cache.put(:foo, 'bar')
cache.include?(:foo) # => true

Parameters:

  • key (Object)

    the key to check for existence.

Returns:

  • (Boolean)

    true if the key exists, false otherwise.



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.

Examples:

cache = Cache.new
cache.merge!({foo: 'bar', baz: 'qux'}) # merges the hash into the cache

Parameters:

  • h (Hash)

    the hash to merge into the cache.

Returns:

  • (Hash)

    the updated records hash.



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.

Examples:

cache = Cache.new
cache.put(:foo, 'bar')

Parameters:

  • key (Object)

    the key to associate with the value.

  • value (Object)

    the value to store in the cache.

Returns:

  • (Cache)

    the Cache instance for method chaining.



28
29
30
31
# File 'lib/gemstone/infomon/cache.rb', line 28

def put(key, value)
  @records[key] = value
  self
end

#to_aArray

Converts the cache records to an array of key-value pairs.

Examples:

cache = Cache.new
cache.put(:foo, 'bar')
cache.to_a # => [[:foo, 'bar']]

Returns:

  • (Array)

    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_hHash

Converts the cache records to a hash.

Examples:

cache = Cache.new
cache.put(:foo, 'bar')
cache.to_h # => {:foo => 'bar'}

Returns:

  • (Hash)

    the hash of records.



122
123
124
# File 'lib/gemstone/infomon/cache.rb', line 122

def to_h()
  @records
end