Class: Documentrix::Documents::MemoryCache

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

Overview

MemoryCache is an in-memory cache implementation for document embeddings.

This class provides a cache store for document embeddings using a hash-based in-memory storage mechanism. It implements the common cache interface defined in Documentrix::Documents::Cache::Common and supports operations such as setting, retrieving, and deleting cached entries, as well as iterating over cached items.

The cache uses a prefix to namespace keys and supports clearing entries based on prefixes or specific tags. It is designed to be used as a temporary storage mechanism during processing and is not persistent across application restarts.

Instance Attribute Summary

Attributes included from Cache::Common

#prefix

Instance Method Summary collapse

Methods included from Cache::Common

#clear, #clear_by_source, #clear_for_tags, #collections, #each_source, #find_records, #pre, #source_exist?, #tags, #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:

  • prefix (String)

    the string to be used as the prefix for this cache



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

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:

  • key (String)

    the key to look up in the cache

Returns:

  • (Object)

    the cached value, or nil if not found



33
34
35
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 33

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:

  • key (String)

    the key to set

  • value (Hash)

    the value to associate with the key



43
44
45
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 43

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

#clear_all_with_prefixDocumentrix::Documents::MemoryCache

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



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

def clear_all_with_prefix
  @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:

  • key (String)

    the key of the value to be deleted

Returns:

  • (TrueClass, FalseClass)

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



62
63
64
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 62

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

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

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



108
109
110
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 108

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

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

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



116
117
118
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 116

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:

  • key (String)

    the key to check for existence

Returns:

  • (TrueClass, FalseClass)

    true if the key exists, false otherwise



52
53
54
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 52

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

#move_prefix(old_prefix, new_prefix) ⇒ Documentrix::Documents::MemoryCache

Move all keys that start with +old_prefix+ into a new prefix.

This helper is used when a collection is renamed. It iterates over all cached entries, selects those whose keys start with old_prefix, strips that prefix, then re‑inserts the entry with the new_prefix instead.

Parameters:

  • old_prefix (String)

    The prefix to look for on existing keys.

  • new_prefix (String)

    The prefix that will replace old_prefix.

Returns:



93
94
95
96
97
98
99
100
101
102
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 93

def move_prefix(old_prefix, new_prefix)
  new_data = @data.dup
  full_each do |key, value|
    key.start_with?(old_prefix) or next
    unpre_key = unpre(key, prefix: old_prefix)
    new_data[pre(unpre_key, prefix: new_prefix)] = new_data.delete(key)
  end
  @data.replace(new_data)
  self
end

#sizeInteger

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

Returns:

  • (Integer)

    The count of elements in the cache.



70
71
72
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 70

def size
  count
end