Class: Documentrix::Documents::RedisCache
- Inherits:
-
Object
- Object
- Documentrix::Documents::RedisCache
- Includes:
- Cache::Common
- Defined in:
- lib/documentrix/documents/cache/redis_cache.rb
Overview
RedisCache is a cache implementation that uses Redis for storing document embeddings and related metadata.
This class provides a persistent cache storage solution for document embeddings, leveraging Redis's capabilities to store both the embedding vectors and associated text data, tags, and source information. It supports efficient vector similarity searches through Redis-based operations.
Instance Attribute Summary collapse
-
#object_class ⇒ Object
readonly
the class of objects stored in the cache.
Attributes included from Cache::Common
Instance Method Summary collapse
- #[](key) ⇒ Object?
-
#[]=(key, value) ⇒ Object
The []= method sets the value associated with the given key in this cache instance.
-
#clear_all_with_prefix ⇒ Documentrix::Documents::RedisCache
The clear_all_with_prefix method removes all key-value pairs associated with the given prefix from this cache instance.
-
#collections(prefix) ⇒ Array<Symbol>
Returns an array of collection names that match the given prefix.
-
#delete(key) ⇒ FalseClass, TrueClass
The delete method removes the key-value pair associated with the given key from this cache instance.
-
#each {|key, value| ... } ⇒ self
The each method iterates over the cache keys with prefix
prefixand yields each key-value pair to the given block. -
#full_each(prefix: 'Documents-') {|key, value| ... } ⇒ Object
The full_each method iterates over all records in the cache and yields them to the block.
-
#initialize(prefix:, url: ENV['REDIS_URL'], object_class:) ⇒ RedisCache
constructor
The initialize method sets up the Documentrix::Documents::RedisCache instance's by setting its prefix attribute to the given value and initializing the Redis client.
-
#key?(key) ⇒ FalseClass, TrueClass
The key? method checks if the given key exists in Redis by calling the redis.exists? method.
-
#move_prefix(old_prefix, new_prefix) ⇒ self
Renames all keys that start with old_prefix to use new_prefix.
-
#redis ⇒ Redis
The redis method returns an instance of Redis client.
-
#set(key, value) ⇒ Object
The set method sets the value associated with the given key in this cache instance.
-
#size ⇒ Integer
The size method returns the total number of keys stored in this cache instance, that is the ones with the prefix
prefix.
Methods included from Cache::Common
#clear, #clear_by_source, #clear_for_tags, #each_source, #find_records, #pre, #source_exist?, #tags, #unpre
Methods included from Utils::Math
#convert_to_vector, #cosine_similarity, #norm
Constructor Details
#initialize(prefix:, url: ENV['REDIS_URL'], object_class:) ⇒ RedisCache
The initialize method sets up the Documentrix::Documents::RedisCache instance's by setting its prefix attribute to the given value and initializing the Redis client.
26 27 28 29 30 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 26 def initialize(prefix:, url: ENV['REDIS_URL'], object_class:) super(prefix:) url or raise ArgumentError, 'require redis url' @url, @object_class = url, object_class end |
Instance Attribute Details
#object_class ⇒ Object (readonly)
the class of objects stored in the cache
32 33 34 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 32 def object_class @object_class end |
Instance Method Details
#[](key) ⇒ Object?
46 47 48 49 50 51 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 46 def [](key) value = redis.get(pre(key)) unless value.nil? JSON.parse(value, object_class:) end end |
#[]=(key, value) ⇒ Object
The []= method sets the value associated with the given key in this cache instance.
59 60 61 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 59 def []=(key, value) set(key, value) end |
#clear_all_with_prefix ⇒ Documentrix::Documents::RedisCache
The clear_all_with_prefix method removes all key-value pairs associated with the given prefix from this cache instance.
123 124 125 126 127 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 123 def clear_all_with_prefix redis.scan_each(match: "#@prefix*") { |key| redis.del(key) } defined? super and super self end |
#collections(prefix) ⇒ Array<Symbol>
Returns an array of collection names that match the given prefix. This is a high-performance override for Redis that only queries keys.
109 110 111 112 113 114 115 116 117 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 109 def collections(prefix) unique = Set.new redis.scan_each(match: "#{prefix}*") do |key| if key =~ /\A#{prefix}(.+)-/ unique << $1.to_sym end end unique.to_a end |
#delete(key) ⇒ FalseClass, TrueClass
The delete method removes the key-value pair associated with the given key from this cache instance.
90 91 92 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 90 def delete(key) redis.del(pre(key)) == 1 end |
#each {|key, value| ... } ⇒ self
The each method iterates over the cache keys with prefix prefix and
yields each key-value pair to the given block.
155 156 157 158 159 160 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 155 def each(&block) block or return enum_for(__method__) redis.scan_each(match: "#@prefix*") { |key| block.(key, self[unpre(key)]) } self end |
#full_each(prefix: 'Documents-') {|key, value| ... } ⇒ Object
The full_each method iterates over all records in the cache and yields them to the block.
166 167 168 169 170 171 172 173 174 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 166 def full_each(prefix: 'Documents-', &block) block or return enum_for(__method__, prefix:) redis.scan_each(match: prefix + ?*) do |key| value = redis.get(key) or next value = JSON.parse(value, object_class:) block.(key, value) end end |
#key?(key) ⇒ FalseClass, TrueClass
The key? method checks if the given key exists in Redis by calling the redis.exists? method
80 81 82 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 80 def key?(key) !!redis.exists?(pre(key)) end |
#move_prefix(old_prefix, new_prefix) ⇒ self
Renames all keys that start with old_prefix to use new_prefix. The method iterates over every affected key, reconstructs the new key name (preserving the part of the key that follows the old prefix), writes the value under the new name, and deletes the old key.
139 140 141 142 143 144 145 146 147 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 139 def move_prefix(old_prefix, new_prefix) full_each(prefix: '') do |key, value| key.start_with?(old_prefix) or next unpre_key = unpre(key, prefix: old_prefix) redis.set(pre(unpre_key, prefix: new_prefix), JSON.generate(value)) redis.del(key) end self end |
#redis ⇒ Redis
The redis method returns an instance of Redis client
37 38 39 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 37 def redis @redis ||= Redis.new(url: @url) end |
#set(key, value) ⇒ Object
The set method sets the value associated with the given key in this cache instance.
69 70 71 72 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 69 def set(key, value) redis.set(pre(key), JSON.generate(value)) value end |
#size ⇒ Integer
The size method returns the total number of keys stored in this cache
instance, that is the ones with the prefix prefix.
98 99 100 101 102 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 98 def size s = 0 redis.scan_each(match: "#@prefix*") { |key| s += 1 } s end |