Class: Documentrix::Documents::RedisBackedMemoryCache

Inherits:
MemoryCache
  • Object
show all
Defined in:
lib/documentrix/documents/cache/redis_backed_memory_cache.rb

Instance Attribute Summary collapse

Attributes included from Cache::Common

#prefix

Instance Method Summary collapse

Methods inherited from MemoryCache

#[], #each, #full_each, #key?, #size

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) ⇒ RedisBackedMemoryCache

The initialize method sets up the RedisBackedMemoryCache cache by creating a new instance and populating it with data from the internally created RedisCache.

Parameters:

  • prefix (String)

    The prefix for keys in the Redis 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 to use for deserializing values from Redis (default: nil)

Raises:

  • (ArgumentError)

    If the redis_url environment variable is not set



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

Parameters:

  • key (String)

    the key to be set

  • value (Hash)

    the hash containing the data to be stored



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

#clearself

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.

Returns:

  • (self)

    self



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.

Parameters:

  • key (String)

    the key to be deleted

Returns:

  • (FalseClass, TrueClass)

    true if the key was successfully deleted, false otherwise.



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

#redisRedis

The redis method returns the Redis client instance used by the cache.

Returns:

  • (Redis)

    The Redis client instance



29
30
31
# File 'lib/documentrix/documents/cache/redis_backed_memory_cache.rb', line 29

def redis
  @redis_cache.redis
end