Class: Documentrix::Documents::RedisCache
- Inherits:
-
Object
- Object
- Documentrix::Documents::RedisCache
- Includes:
- Cache::Common, Enumerable
- Defined in:
- lib/documentrix/documents/cache/redis_cache.rb
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 ⇒ Documentrix::Documents::RedisCache
The clear method removes all key-value pairs associated with the given prefix from this cache instance.
-
#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
prefix
and yields each key-value pair to the given block. -
#initialize(prefix:, url: ENV['REDIS_URL'], object_class: nil, ex: nil) ⇒ 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.
-
#redis ⇒ Redis
The redis method returns an instance of Redis client.
-
#set(key, value, ex: nil) ⇒ 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
. -
#ttl(key) ⇒ Integer?
The ttl method returns the time-to-live (TTL) value for the given key.
Methods included from Cache::Common
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.
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_class ⇒ Object (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?
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.
48 49 50 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 48 def []=(key, value) set(key, value) end |
#clear ⇒ Documentrix::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.
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.
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
84 85 86 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 84 def key?(key) !!redis.exists?(pre(key)) end |
#redis ⇒ Redis
The redis method returns 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.
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 |
#size ⇒ Integer
The size method returns the total number of keys stored in this cache
instance, that is the ones with the prefix prefix
.
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
74 75 76 |
# File 'lib/documentrix/documents/cache/redis_cache.rb', line 74 def ttl(key) redis.ttl(pre(key)) end |