Class: Documentrix::Documents::MemoryCache
- Inherits:
-
Object
- Object
- Documentrix::Documents::MemoryCache
- 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
Instance Method Summary collapse
-
#[](key) ⇒ Object
The [] method retrieves the value associated with the given key from the cache.
-
#[]=(key, value) ⇒ void
The []= method sets the value for a given key in the cache.
-
#clear_all_with_prefix ⇒ Documentrix::Documents::MemoryCache
The clear_all_with_prefix method removes all records from the cache that have keys starting with the prefix
prefix. -
#delete(key) ⇒ TrueClass, FalseClass
The delete method removes the key-value pair from the cache by deleting it from the underlying data structure.
-
#each {|key, value| ... } ⇒ Object
The each method iterates over the cache's keys and values under a given prefix
prefix. -
#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. -
#initialize(prefix:) ⇒ MemoryCache
constructor
The initialize method sets up the Documentrix::Documents::Cache instance's by setting its prefix attribute to the given value.
-
#key?(key) ⇒ TrueClass, FalseClass
The key? method checks if the given key exists in the cache.
-
#move_prefix(old_prefix, new_prefix) ⇒ Documentrix::Documents::MemoryCache
Move all keys that start with +old_prefix+ into a new prefix.
-
#size ⇒ Integer
The size method returns the number of elements in the cache, that is the ones prefixed with
prefix.
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.
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.
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.
43 44 45 |
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 43 def []=(key, value) @data[pre(key)] = value end |
#clear_all_with_prefix ⇒ Documentrix::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.
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.
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.
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.
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.
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 |
#size ⇒ Integer
The size method returns the number of elements in the cache, that is the
ones prefixed with prefix.
70 71 72 |
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 70 def size count end |