Class: Ollama::Utils::CacheFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama/utils/cache_fetcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(cache) ⇒ CacheFetcher

Returns a new instance of CacheFetcher.



4
5
6
# File 'lib/ollama/utils/cache_fetcher.rb', line 4

def initialize(cache)
  @cache = cache
end

Instance Method Details

#get(url, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ollama/utils/cache_fetcher.rb', line 8

def get(url, &block)
  block or raise ArgumentError, 'require block argument'
  body         = @cache[key(:body, url)]
  content_type = @cache[key(:content_type, url)]
  content_type = MIME::Types[content_type].first
  if body && content_type
    io = StringIO.new(body)
    io.rewind
    io.extend(Ollama::Utils::Fetcher::HeaderExtension)
    io.content_type = content_type
    block.(io)
  end
end

#put(url, io) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/ollama/utils/cache_fetcher.rb', line 22

def put(url, io)
  io.rewind
  body = io.read
  body.empty? and return
  content_type = io.content_type
  content_type.nil? and return
  @cache.set(key(:body, url), body, ex: io.ex)
  @cache.set(key(:content_type,  url), content_type.to_s, ex: io.ex)
  self
end