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

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

Examples:

Creating a cache and using it

cache = Lich::Gemstone::Infomon::Cache.new
cache.put(:key, "value")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Initializes a new Cache instance



16
17
18
# File 'documented/gemstone/infomon/cache.rb', line 16

def initialize()
  @records = {}
end

Instance Attribute Details

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

Examples:

Deleting a key from the cache

cache.delete(:key)

Parameters:

  • key (Object)

    The key of the value to delete

Returns:

  • (Object, nil)

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



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

Examples:

Flushing the cache

cache.flush!


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.

Examples:

Getting a value from the cache

value = cache.get(:key) { fetch_from_db(:key) }

Parameters:

  • key (Object)

    The key of the value to retrieve

Returns:

  • (Object, nil)

    The value associated with the key, or nil if not found



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

Examples:

Checking for a key in the cache

cache.include?(:key)

Parameters:

  • key (Object)

    The key to check for

Returns:

  • (Boolean)

    True if the key exists in the cache, false otherwise



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

Examples:

Merging a hash into the cache

cache.merge!({:key1 => "value1", :key2 => "value2"})

Parameters:

  • h (Hash)

    The hash to merge into the cache

Returns:

  • (Hash)

    The updated records in 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

Examples:

Putting a value in the cache

cache.put(:key, "value")

Parameters:

  • key (Object)

    The key to store the value under

  • value (Object)

    The value to store

Returns:

  • (Cache)

    The current instance of Cache



26
27
28
29
# File 'documented/gemstone/infomon/cache.rb', line 26

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

#to_aArray

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

Examples:

Converting cache records to an array

array = cache.to_a

Returns:

  • (Array)

    An array representation of the cache records



85
86
87
# File 'documented/gemstone/infomon/cache.rb', line 85

def to_a()
  @records.to_a
end

#to_hHash

Returns the cache records as a hash

Examples:

Getting the cache records as a hash

hash = cache.to_h

Returns:

  • (Hash)

    The hash representation of the cache records



93
94
95
# File 'documented/gemstone/infomon/cache.rb', line 93

def to_h()
  @records
end