Class: Documentrix::Documents::MemoryCache
- Inherits:
-
Object
- Object
- Documentrix::Documents::MemoryCache
- Includes:
- Cache::Common, Enumerable
- Defined in:
- lib/documentrix/documents/cache/memory_cache.rb
Direct Known Subclasses
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 ⇒ Documentrix::Documents::MemoryCache
The clear 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| ... } ⇒ void
The each method iterates over the cache's keys and values under a given prefix
prefix. -
#full_each {|key, value| ... } ⇒ void
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.
-
#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
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.
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.
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.
31 32 33 |
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 31 def []=(key, value) @data[pre(key)] = value end |
#clear ⇒ Documentrix::Documents::MemoryCache
The clear method removes all records from the cache that have keys starting
with the prefix prefix.
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.
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.
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.
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.
40 41 42 |
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 40 def key?(key) @data.key?(pre(key)) end |
#size ⇒ Integer
The size method returns the number of elements in the cache, that is the
ones prefixed with prefix.
58 59 60 |
# File 'lib/documentrix/documents/cache/memory_cache.rb', line 58 def size count end |