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

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

Instance Method Summary collapse

Constructor Details

#initializeCache

Initializes a new Cache instance.



14
15
16
# File 'documented/gemstone/infomon/cache.rb', line 14

def initialize()
  @records = {}
end

Instance Attribute Details

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

Parameters:

  • key (String)

    the key to delete

Returns:

  • (Object, nil)

    the deleted value, or nil if the key was not found



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.

Parameters:

  • key (String)

    the key to retrieve the value for

Yield Parameters:

  • key (String)

    the key that was not found in the cache

Returns:

  • (Object, nil)

    the value associated with the key, or nil if not found and no block is 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.

Parameters:

  • key (String)

    the key to check

Returns:

  • (Boolean)

    true if the key exists in the cache, false otherwise



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.

Parameters:

  • h (Hash)

    a hash of key-value pairs to merge into the cache

Returns:

  • (Hash)

    the updated records in 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.

Parameters:

  • key (String)

    the key to store the value under

  • value (Object)

    the value to store

Returns:

  • (Cache)

    the Cache instance for method chaining



22
23
24
25
# File 'documented/gemstone/infomon/cache.rb', line 22

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

#to_aArray<Array>

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

Returns:

  • (Array<Array>)

    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_hHash

Returns the cache records as a hash.

Returns:

  • (Hash)

    the records in the cache



76
77
78
# File 'documented/gemstone/infomon/cache.rb', line 76

def to_h()
  @records
end