Class: Ollama::Documents::RedisBackedMemoryCache

Inherits:
MemoryCache
  • Object
show all
Defined in:
lib/ollama/documents/cache/redis_backed_memory_cache.rb

Instance Attribute Summary

Attributes included from Cache::Common

#prefix

Instance Method Summary collapse

Methods inherited from MemoryCache

#[], #each, #full_each, #key?, #size

Methods included from Cache::Common

#collections

Constructor Details

#initialize(prefix:, url: ENV['REDIS_URL']) ⇒ RedisBackedMemoryCache

Returns a new instance of RedisBackedMemoryCache.



5
6
7
8
9
10
11
12
13
# File 'lib/ollama/documents/cache/redis_backed_memory_cache.rb', line 5

def initialize(prefix:, url: ENV['REDIS_URL'])
  super(prefix:)
  url or raise ArgumentError, 'require redis url'
  @prefix, @url = prefix, url
  @redis_cache  = Ollama::Documents::RedisCache.new(prefix:, url:)
  @redis_cache.full_each do |key, value|
    @data[key] = value
  end
end

Instance Method Details

#[]=(key, value) ⇒ Object



19
20
21
22
# File 'lib/ollama/documents/cache/redis_backed_memory_cache.rb', line 19

def []=(key, value)
  super
  redis.set(pre(key), JSON(value))
end

#clearObject



30
31
32
33
34
# File 'lib/ollama/documents/cache/redis_backed_memory_cache.rb', line 30

def clear
  redis.scan_each(match: "#@prefix*") { |key| redis.del(key) }
  super
  self
end

#delete(key) ⇒ Object



24
25
26
27
28
# File 'lib/ollama/documents/cache/redis_backed_memory_cache.rb', line 24

def delete(key)
  result = redis.del(pre(key))
  super
  result
end

#pre(key) ⇒ Object



36
37
38
# File 'lib/ollama/documents/cache/redis_backed_memory_cache.rb', line 36

def pre(key)
  [ @prefix, key ].join
end

#redisObject



15
16
17
# File 'lib/ollama/documents/cache/redis_backed_memory_cache.rb', line 15

def redis
  @redis_cache.redis
end

#unpre(key) ⇒ Object



40
41
42
# File 'lib/ollama/documents/cache/redis_backed_memory_cache.rb', line 40

def unpre(key)
  key.sub(/\A#@prefix/, '')
end