Class: Documentrix::Documents::RedisCache

Inherits:
Object
  • Object
show all
Includes:
Cache::Common, Enumerable
Defined in:
lib/documentrix/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

Methods included from Utils::Math

#convert_to_vector, #cosine_similarity, #norm

Constructor Details

#initialize(prefix:, url: ENV['REDIS_URL'], object_class: nil, ex: nil) ⇒ 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) (defaults to: nil)

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

  • ex (Integer) (defaults to: nil)

    the expiration time in seconds (default: nil)



15
16
17
18
19
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 15

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

Instance Attribute Details

#object_classObject (readonly)

the class of objects stored in the cache



21
22
23
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 21

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



35
36
37
38
39
40
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 35

def [](key)
  value = redis.get(pre(key))
  unless value.nil?
    object_class ? JSON(value, object_class:) : JSON(value)
  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



48
49
50
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 48

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

#clearDocumentrix::Documents::RedisCache

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



112
113
114
115
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 112

def clear
  redis.scan_each(match: "#@prefix*") { |key| redis.del(key) }
  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



94
95
96
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 94

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



123
124
125
126
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 123

def each(&block)
  redis.scan_each(match: "#@prefix*") { |key| block.(key, self[unpre(key)]) }
  self
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



84
85
86
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 84

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

#redisRedis

The redis method returns an instance of Redis client

Returns:

  • (Redis)

    An instance of Redis client



26
27
28
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 26

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

#set(key, value, ex: nil) ⇒ 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

  • ex (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (ex:):

  • ex (Integer)

    the expiration time in seconds (default: nil)

Returns:

  • (Object)

    self



59
60
61
62
63
64
65
66
67
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 59

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

#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



102
103
104
105
106
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 102

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

#ttl(key) ⇒ Integer?

The ttl method returns the time-to-live (TTL) value for the given key

Parameters:

  • key (String)

    the string representation of the key

Returns:

  • (Integer, nil)

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



74
75
76
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 74

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