Class: Documentrix::Documents::RedisBackedMemoryCache
- Inherits:
-
MemoryCache
- Object
- MemoryCache
- Documentrix::Documents::RedisBackedMemoryCache
- Defined in:
- lib/documentrix/documents/cache/redis_backed_memory_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, value) ⇒ Object
The set method sets the value for a given key in memory and in Redis.
-
#clear ⇒ self
The clear method deletes all keys from the cache by scanning redis for keys that match the prefix
prefix
and then deleting them, then it does the same for the MemoryCache by calling its super. -
#delete(key) ⇒ FalseClass, TrueClass
The delete method removes a key from the cache by calling Redis's del method and then calling the superclass's delete method.
-
#initialize(prefix:, url: ENV['REDIS_URL'], object_class: nil) ⇒ RedisBackedMemoryCache
constructor
The initialize method sets up the RedisBackedMemoryCache cache by creating a new instance and populating it with data from the internally created RedisCache.
-
#redis ⇒ Redis
The redis method returns the Redis client instance used by the cache.
Methods inherited from MemoryCache
#[], #each, #full_each, #key?, #size
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) ⇒ RedisBackedMemoryCache
The initialize method sets up the RedisBackedMemoryCache cache by creating a new instance and populating it with data from the internally created RedisCache.
15 16 17 18 19 20 21 22 |
# File 'lib/documentrix/documents/cache/redis_backed_memory_cache.rb', line 15 def initialize(prefix:, url: ENV['REDIS_URL'], object_class: nil) super(prefix:) url or raise ArgumentError, 'require redis url' @url, @object_class = url, object_class @redis_cache = Documentrix::Documents::RedisCache.new(prefix:, url:, object_class:) @redis_cache.extend(Documentrix::Documents::Cache::Records::RedisFullEach) @redis_cache.full_each { |key, value| @data[key] = value } end |
Instance Attribute Details
#object_class ⇒ Object (readonly)
the class of objects stored in the cache
24 25 26 |
# File 'lib/documentrix/documents/cache/redis_backed_memory_cache.rb', line 24 def object_class @object_class end |
Instance Method Details
#[]=(key, value) ⇒ Object
The set method sets the value for a given key in memory and in Redis.
37 38 39 40 |
# File 'lib/documentrix/documents/cache/redis_backed_memory_cache.rb', line 37 def []=(key, value) super redis.set(pre(key), JSON(value)) end |
#clear ⇒ self
The clear method deletes all keys from the cache by scanning redis for
keys that match the prefix prefix
and then deleting them, then it does
the same for the MemoryCache by calling its super.
58 59 60 61 62 |
# File 'lib/documentrix/documents/cache/redis_backed_memory_cache.rb', line 58 def clear redis.scan_each(match: "#@prefix*") { |key| redis.del(key) } super self end |
#delete(key) ⇒ FalseClass, TrueClass
The delete method removes a key from the cache by calling Redis's del method and then calling the superclass's delete method.
48 49 50 51 |
# File 'lib/documentrix/documents/cache/redis_backed_memory_cache.rb', line 48 def delete(key) result = redis.del(pre(key)) super && result == 1 end |
#redis ⇒ Redis
The redis method returns the Redis client instance used by the cache.
29 30 31 |
# File 'lib/documentrix/documents/cache/redis_backed_memory_cache.rb', line 29 def redis @redis_cache.redis end |