Class: Ollama::Documents::MemoryCache

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Cache::Common
Defined in:
lib/ollama/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

Constructor Details

#initialize(prefix:) ⇒ MemoryCache

Returns a new instance of MemoryCache.



6
7
8
9
# File 'lib/ollama/documents/cache/memory_cache.rb', line 6

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

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



15
16
17
# File 'lib/ollama/documents/cache/memory_cache.rb', line 15

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

#clearObject



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

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

#delete(key) ⇒ Object



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

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

#each(&block) ⇒ Object



36
37
38
# File 'lib/ollama/documents/cache/memory_cache.rb', line 36

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

#full_each(&block) ⇒ Object



41
42
43
# File 'lib/ollama/documents/cache/memory_cache.rb', line 41

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/ollama/documents/cache/memory_cache.rb', line 19

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

#sizeObject



27
28
29
# File 'lib/ollama/documents/cache/memory_cache.rb', line 27

def size
  count
end