Sha256: b05474248eea872472ab2df549e5aa3bec08732c7cf90e5c9c7065f4381766be

Contents?: true

Size: 904 Bytes

Versions: 11

Compression:

Stored size: 904 Bytes

Contents

require 'digest/md5'

class Ollama::Utils::CacheFetcher
  def initialize(cache)
    @cache = cache
  end

  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

  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

  private

  def key(type, url)
    [ type, Digest::MD5.hexdigest(url) ] * ?-
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ollama-ruby-0.13.0 lib/ollama/utils/cache_fetcher.rb
ollama-ruby-0.12.1 lib/ollama/utils/cache_fetcher.rb
ollama-ruby-0.12.0 lib/ollama/utils/cache_fetcher.rb
ollama-ruby-0.11.0 lib/ollama/utils/cache_fetcher.rb
ollama-ruby-0.10.0 lib/ollama/utils/cache_fetcher.rb
ollama-ruby-0.9.3 lib/ollama/utils/cache_fetcher.rb
ollama-ruby-0.9.2 lib/ollama/utils/cache_fetcher.rb
ollama-ruby-0.9.1 lib/ollama/utils/cache_fetcher.rb
ollama-ruby-0.9.0 lib/ollama/utils/cache_fetcher.rb
ollama-ruby-0.8.0 lib/ollama/utils/cache_fetcher.rb
ollama-ruby-0.7.0 lib/ollama/utils/cache_fetcher.rb