Class: OllamaChat::ModelHandling::ModelCache
- Inherits:
-
Object
- Object
- OllamaChat::ModelHandling::ModelCache
- Defined in:
- lib/ollama_chat/model_handling/model_cache.rb
Overview
Caches model metadata for one or more Ollama instances.
This class provides a simple in-memory cache to store and retrieve
model metadata, reducing the need for repeated ollama show calls.
It supports multiple Ollama instances by using the instance's
object ID as part of the cache key.
Instance Method Summary collapse
-
#cache(ollama, name, value = nil, size: nil) ⇒ ModelMetadata?
Caches model metadata (and optionally size) for a given Ollama instance and model name.
-
#cached?(ollama, name) ⇒ ModelMetadata?
Returns the cached
ModelMetadatafor a given model, if it has been fetched. -
#flush(ollama = nil) ⇒ true
Flushes cached metadata and sizes for one or all Ollama instances.
-
#initialize ⇒ ModelCache
constructor
Initializes a new model cache.
-
#model_size(ollama, name) ⇒ Integer
Returns the cached size in bytes for a given model.
-
#models(ollama) ⇒ Array<String>
Returns a list of cached model names for the given Ollama instance.
-
#register_all(ollama) ⇒ ModelCache
private
Registers multiple model names as cached for the given Ollama instance.
-
#registered?(ollama) ⇒ Boolean
private
Checks whether model registration has been completed for the instance.
Constructor Details
#initialize ⇒ ModelCache
Initializes a new model cache.
10 11 12 13 14 |
# File 'lib/ollama_chat/model_handling/model_cache.rb', line 10 def initialize @models = Hash.new { |h, k| h[k] = {} } @sizes = Hash.new { |h, k| h[k] = {} } @registered = Hash.new(false) end |
Instance Method Details
#cache(ollama, name, value = nil, size: nil) ⇒ ModelMetadata?
Caches model metadata (and optionally size) for a given Ollama instance and model name.
Existing metadata is never overwritten (||=); only missing
entries are filled in. Size, when provided, overwrites unconditionally.
27 28 29 30 31 32 |
# File 'lib/ollama_chat/model_handling/model_cache.rb', line 27 def cache(ollama, name, value = nil, size: nil) name = name.to_s @models[ollama.__id__][name] ||= value size and @sizes[ollama.__id__][name] = size @models[ollama.__id__][name] end |
#cached?(ollama, name) ⇒ ModelMetadata?
Returns the cached ModelMetadata for a given model, if it has
been fetched. Note: this is a metadata predicate, not an
existence check. A model can be registered (via models) and
have a size, yet still return nil here until metadata is
explicitly cached (e.g. by model_present?).
43 44 45 46 |
# File 'lib/ollama_chat/model_handling/model_cache.rb', line 43 def cached?(ollama, name) name = name.to_s @models[ollama.__id__][name] end |
#flush(ollama = nil) ⇒ true
Flushes cached metadata and sizes for one or all Ollama instances.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ollama_chat/model_handling/model_cache.rb', line 71 def flush(ollama = nil) if ollama ollama_id = ollama.ask_and_send(:to_int) || ollama.__id__ @models[ollama_id].clear @sizes[ollama_id].clear @registered[ollama_id] = false else @models.each_key { |ollama_id| flush(ollama_id) } end true end |
#model_size(ollama, name) ⇒ Integer
Returns the cached size in bytes for a given model.
53 54 55 |
# File 'lib/ollama_chat/model_handling/model_cache.rb', line 53 def model_size(ollama, name) @sizes[ollama.__id__][name].to_i end |
#models(ollama) ⇒ Array<String>
Returns a list of cached model names for the given Ollama instance.
61 62 63 64 |
# File 'lib/ollama_chat/model_handling/model_cache.rb', line 61 def models(ollama) registered?(ollama) or register_all(ollama) @models[ollama.__id__].keys end |
#register_all(ollama) ⇒ ModelCache (private)
Registers multiple model names as cached for the given Ollama instance.
98 99 100 101 102 103 104 |
# File 'lib/ollama_chat/model_handling/model_cache.rb', line 98 def register_all(ollama) ollama..models.each do |model| cache(ollama, model.name, size: model.size) end @registered[ollama.__id__] = true self end |
#registered?(ollama) ⇒ Boolean (private)
Checks whether model registration has been completed for the instance.
89 90 91 |
# File 'lib/ollama_chat/model_handling/model_cache.rb', line 89 def registered?(ollama) @registered[ollama.__id__] end |