Class: RobotLab::RedisBackend Private

Inherits:
Object
  • Object
show all
Defined in:
lib/robot_lab/memory.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Redis backend for Memory (optional, loaded when Redis is available)

Instance Method Summary collapse

Constructor Details

#initializeRedisBackend

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of RedisBackend.



911
912
913
914
# File 'lib/robot_lab/memory.rb', line 911

def initialize
  @redis = create_redis_connection
  @namespace = "robot_lab:memory:#{SecureRandom.uuid}"
end

Instance Method Details

#[](key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



916
917
918
919
920
921
# File 'lib/robot_lab/memory.rb', line 916

def [](key)
  value = @redis.get("#{@namespace}:#{key}")
  value ? JSON.parse(value, symbolize_names: true) : nil
rescue JSON::ParserError
  value
end

#[]=(key, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



923
924
925
926
# File 'lib/robot_lab/memory.rb', line 923

def []=(key, value)
  serialized = value.is_a?(String) ? value : value.to_json
  @redis.set("#{@namespace}:#{key}", serialized)
end

#clearObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



942
943
944
# File 'lib/robot_lab/memory.rb', line 942

def clear
  keys.each { |k| delete(k) }
end

#delete(key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



936
937
938
939
940
# File 'lib/robot_lab/memory.rb', line 936

def delete(key)
  value = self[key]
  @redis.del("#{@namespace}:#{key}")
  value
end

#key?(key) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


928
929
930
# File 'lib/robot_lab/memory.rb', line 928

def key?(key)
  @redis.exists?("#{@namespace}:#{key}")
end

#keysObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



932
933
934
# File 'lib/robot_lab/memory.rb', line 932

def keys
  @redis.keys("#{@namespace}:*").map { |k| k.sub("#{@namespace}:", "").to_sym }
end