Class: Documentrix::Documents::MemoryCache

Inherits:
Object
  • Object
show all
Includes:
Cache::Common, Enumerable
Defined in:
lib/documentrix/documents/cache/memory_cache.rb

Direct Known Subclasses

RedisBackedMemoryCache

Instance Attribute Summary

Attributes included from Cache::Common

#prefix

Instance Method Summary collapse

Methods included from Cache::Common

#collections, #pre, #unpre

Methods included from Utils::Math

#convert_to_vector, #cosine_similarity, #norm

Constructor Details

#initialize(prefix:) ⇒ MemoryCache

The initialize method sets up the Documentrix::Documents::Cache instance's by setting its prefix attribute to the given value.

Parameters:

  • the string to be used as the prefix for this cache



10
11
12
13
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 10

def initialize(prefix:)
  super(prefix:)
  @data   = {}
end

Instance Method Details

#[](key) ⇒ Object

The [] method retrieves the value associated with the given key from the cache.

Parameters:

  • the key to look up in the cache

Returns:

  • the cached value, or nil if not found



21
22
23
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 21

def [](key)
  @data[pre(key)]
end

#[]=(key, value) ⇒ void

This method returns an undefined value.

The []= method sets the value for a given key in the cache.

Parameters:

  • the key to set

  • the value to associate with the key



31
32
33
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 31

def []=(key, value)
  @data[pre(key)] = value
end

#clearDocumentrix::Documents::MemoryCache

The clear method removes all records from the cache that have keys starting with the prefix prefix.

Returns:

  • self



66
67
68
69
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 66

def clear
  @data.delete_if { |key, _| key.start_with?(@prefix) }
  self
end

#delete(key) ⇒ TrueClass, FalseClass

The delete method removes the key-value pair from the cache by deleting it from the underlying data structure.

Parameters:

  • the key of the value to be deleted

Returns:

  • true if the key was found and deleted, false otherwise.



50
51
52
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 50

def delete(key)
  !!@data.delete(pre(key))
end

#each {|key, value| ... } ⇒ void

This method returns an undefined value.

The each method iterates over the cache's keys and values under a given prefix prefix.

Yields:

  • (key, value)

    Each key-value pair in the cache



77
78
79
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 77

def each(&block)
  @data.select { |key,| key.start_with?(@prefix) }.each(&block)
end

#full_each {|key, value| ... } ⇒ void

This method returns an undefined value.

The full_each method iterates over the data hash and yields each key-value pair to the given block regardless of the prefix prefix.

Yields:

  • (key, value)

    Each key-value pair in the data hash



88
89
90
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 88

def full_each(&block)
  @data.each(&block)
end

#key?(key) ⇒ TrueClass, FalseClass

The key? method checks if the given key exists in the cache.

Parameters:

  • the key to check for existence

Returns:

  • true if the key exists, false otherwise



40
41
42
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 40

def key?(key)
  @data.key?(pre(key))
end

#sizeInteger

The size method returns the number of elements in the cache, that is the ones prefixed with prefix.

Returns:

  • The count of elements in the cache.



58
59
60
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 58

def size
  count
end