Class: Ollama::Documents::RedisCache

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Cache::Common
Defined in:
lib/ollama/documents/cache/redis_cache.rb

Instance Attribute Summary collapse

Attributes included from Cache::Common

#prefix

Instance Method Summary collapse

Methods included from Cache::Common

#collections, #pre, #unpre

Constructor Details

#initialize(prefix:, url: ENV['REDIS_URL'], object_class: nil, ex: nil) ⇒ RedisCache

Returns a new instance of RedisCache.



7
8
9
10
# File 'lib/ollama/documents/cache/redis_cache.rb', line 7

def initialize(prefix:, url: ENV['REDIS_URL'], object_class: nil, ex: nil)
  url or raise ArgumentError, 'require redis url'
  @prefix, @url, @object_class, @ex = prefix, url, object_class, ex
end

Instance Attribute Details

#object_classObject (readonly)

Returns the value of attribute object_class.



12
13
14
# File 'lib/ollama/documents/cache/redis_cache.rb', line 12

def object_class
  @object_class
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  value = redis.get(pre(key))
  unless value.nil?
    JSON(value, object_class:)
  end
end

#[]=(key, value) ⇒ Object



25
26
27
# File 'lib/ollama/documents/cache/redis_cache.rb', line 25

def []=(key, value)
  set(key, value)
end

#clearObject



57
58
59
60
# File 'lib/ollama/documents/cache/redis_cache.rb', line 57

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

#delete(key) ⇒ Object



47
48
49
# File 'lib/ollama/documents/cache/redis_cache.rb', line 47

def delete(key)
  redis.del(pre(key)) == 1
end

#each(&block) ⇒ Object



62
63
64
65
# File 'lib/ollama/documents/cache/redis_cache.rb', line 62

def each(&block)
  redis.scan_each(match: "#@prefix*") { |key| block.(key, self[unpre(key)]) }
  self
end

#full_each(&block) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/ollama/documents/cache/redis_cache.rb', line 68

def full_each(&block)
  redis.scan_each(match: [ Ollama::Documents, ?* ] * ?-) do |key|
    value = redis.get(key) or next
    value = JSON(value, object_class: Ollama::Documents::Record)
    block.(key, value)
  end
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/ollama/documents/cache/redis_cache.rb', line 43

def key?(key)
  !!redis.exists?(pre(key))
end

#redisObject



14
15
16
# File 'lib/ollama/documents/cache/redis_cache.rb', line 14

def redis
  @redis ||= Redis.new(url: @url)
end

#set(key, value, ex: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/ollama/documents/cache/redis_cache.rb', line 29

def set(key, value, ex: nil)
  ex ||= @ex
  if !ex.nil? && ex < 1
    redis.del(pre(key))
  else
    redis.set(pre(key), JSON.generate(value), ex:)
  end
  value
end

#sizeObject



51
52
53
54
55
# File 'lib/ollama/documents/cache/redis_cache.rb', line 51

def size
  s = 0
  redis.scan_each(match: "#@prefix*") { |key| s += 1 }
  s
end

#ttl(key) ⇒ Object



39
40
41
# File 'lib/ollama/documents/cache/redis_cache.rb', line 39

def ttl(key)
  redis.ttl(pre(key))
end