Class: OllamaChat::ModelHandling::ModelCache

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • ollama (Ollama::Client) —

    the Ollama client instance

  • name (String, Symbol) —

    the model name

  • value (ModelMetadata, nil) (defaults to: nil) —

    the metadata to cache (defaults to nil)

  • size (Integer, nil) (defaults to: nil) —

    the model size in bytes (optional)

Returns:



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?).

Parameters:

  • ollama (Ollama::Client) —

    the Ollama client instance

  • name (String, Symbol) —

    the model name

Returns:

  • (ModelMetadata, nil) —

    the metadata if fetched, nil otherwise



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.

Parameters:

  • ollama (Ollama::Client, Integer, nil) (defaults to: nil) —

    an Ollama client instance, a raw object ID, or nil to flush all instances (default: nil)

Returns:

  • (true)


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.

Parameters:

  • ollama (Ollama::Client) —

    the Ollama client instance

  • name (String, Symbol) —

    the model name

Returns:

  • (Integer) —

    the size in bytes, 0 if not cached



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.

Parameters:

  • ollama (Ollama::Client) —

    the Ollama client instance

Returns:

  • (Array<String>) —

    the cached model names



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.

Parameters:

  • ollama (Ollama::Client) —

    the Ollama client instance

Returns:



98
99
100
101
102
103
104
# File 'lib/ollama_chat/model_handling/model_cache.rb', line 98

def register_all(ollama)
  ollama.tags.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.

Parameters:

  • ollama (Ollama::Client) —

    the Ollama client instance

Returns:

  • (Boolean)


89
90
91
# File 'lib/ollama_chat/model_handling/model_cache.rb', line 89

def registered?(ollama)
  @registered[ollama.__id__]
end