Class: Ollama::Middleware::Cache

Inherits:
Ollama::Middleware show all
Defined in:
lib/ollama/middleware/cache.rb

Overview

Cache middleware - caches responses based on request key.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Ollama::Middleware

#around, #on_error

Constructor Details

#initialize(store:, ttl: 300, key_generator: nil, cacheable_endpoints: %i[chat generate embeddings])) ⇒ Cache

Returns a new instance of Cache.



13
14
15
16
17
18
19
20
# File 'lib/ollama/middleware/cache.rb', line 13

def initialize(store:, ttl: 300, key_generator: nil,
               cacheable_endpoints: %i[chat generate embeddings])
  super()
  @store = store
  @ttl = ttl
  @key_generator = key_generator || ->(request) { default_key(request) }
  @cacheable_endpoints = Array(cacheable_endpoints).map(&:to_sym)
end

Instance Attribute Details

#cacheable_endpointsObject (readonly)

Returns the value of attribute cacheable_endpoints.



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

def cacheable_endpoints
  @cacheable_endpoints
end

#key_generatorObject (readonly)

Returns the value of attribute key_generator.



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

def key_generator
  @key_generator
end

#storeObject (readonly)

Returns the value of attribute store.



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

def store
  @store
end

#ttlObject (readonly)

Returns the value of attribute ttl.



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

def ttl
  @ttl
end

Instance Method Details

#after_response(response, env) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/ollama/middleware/cache.rb', line 36

def after_response(response, env)
  return response unless cache_write?(env)

  key = @key_generator.call(env[:request])
  return response unless key

  @store.write(key, response, expires_in: @ttl) if response.success?
  response
end

#before_request(request, env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ollama/middleware/cache.rb', line 22

def before_request(request, env)
  return request unless cacheable?(request)

  key = @key_generator.call(request)
  return request unless key

  cached = @store.read(key)
  return request unless cached

  env[:cached_response] = cached
  env[:cache_hit] = true
  request
end