Class: Documentrix::Documents::RedisCache

Inherits:
Object
  • Object
show all
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.

Examples:

cache = Documentrix::Documents::RedisCache.new(prefix: 'docs-', url: 'redis://localhost:6379')
cache['key'] = { text: 'example', embedding: [0.1, 0.2, 0.3] }
value = cache['key']

Instance Attribute Summary collapse

Attributes included from Cache::Common

#prefix

Instance Method Summary collapse

Methods included from Cache::Common

#clear, #clear_by_source, #clear_for_tags, #collections, #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.

Parameters:

  • prefix (String)

    the string to be used as the prefix for this cache

  • url (String) (defaults to: ENV['REDIS_URL'])

    the URL of the Redis server (default: ENV['REDIS_URL'])

  • object_class (Class)

    the class of objects stored in Redis (default: nil)



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_classObject (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?

The method retrieves the value associated with the given key from Redis.

Parameters:

  • key (String)

    the string representation of the key

Returns:

  • (Object, nil)

    the retrieved value if it exists in Redis, or nil otherwise



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.

Parameters:

  • key (String)

    the string representation of the key

  • value (Object)

    the object to be stored under the given key

Returns:

  • (Object)

    self



59
60
61
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 59

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

#clear_all_with_prefixDocumentrix::Documents::RedisCache

The clear_all_with_prefix method removes all key-value pairs associated with the given prefix from this cache instance.



108
109
110
111
112
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 108

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

#delete(key) ⇒ FalseClass, TrueClass

The delete method removes the key-value pair associated with the given key from this cache instance.

Parameters:

  • key (String)

    the string representation of the key

Returns:

  • (FalseClass, TrueClass)

    true if the key was deleted successfully, false otherwise



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.

Yields:

  • (key, value)

    Each key-value pair in the cache

Returns:

  • (self)

    self



140
141
142
143
144
145
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 140

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.

Yields:

  • (key, value)

    where key is the record's key and value is the record itself



151
152
153
154
155
156
157
158
159
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 151

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

Parameters:

  • key (String)

    the string representation of the key

Returns:

  • (FalseClass, TrueClass)

    true if the key exists, false otherwise



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.

Parameters:

  • old_prefix (String)

    The prefix that currently identifies the target keys.

  • new_prefix (String)

    The prefix that should replace old_prefix.

Returns:

  • (self)

    The cache instance, facilitating method chaining.



124
125
126
127
128
129
130
131
132
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 124

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

#redisRedis

The redis method returns an instance of Redis client

Returns:

  • (Redis)

    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.

Parameters:

  • key (String)

    the string representation of the key

  • value (Object)

    the object to be stored under the given key

Returns:

  • (Object)

    self



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

#sizeInteger

The size method returns the total number of keys stored in this cache instance, that is the ones with the prefix prefix.

Returns:

  • (Integer)

    The total count of keys



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