Class: RobotLab::RedisBackend Private
- Inherits:
-
Object
- Object
- RobotLab::RedisBackend
- 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
- #[](key) ⇒ Object private
- #[]=(key, value) ⇒ Object private
- #clear ⇒ Object private
- #delete(key) ⇒ Object private
-
#initialize ⇒ RedisBackend
constructor
private
A new instance of RedisBackend.
- #key?(key) ⇒ Boolean private
- #keys ⇒ Object private
Constructor Details
#initialize ⇒ RedisBackend
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.
905 906 907 908 |
# File 'lib/robot_lab/memory.rb', line 905 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.
910 911 912 913 914 915 |
# File 'lib/robot_lab/memory.rb', line 910 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.
917 918 919 920 921 |
# File 'lib/robot_lab/memory.rb', line 917 def []=(key, value) serialized = value.is_a?(String) ? value : value.to_json @redis.set("#{@namespace}:#{key}", serialized) value end |
#clear ⇒ 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.
937 938 939 |
# File 'lib/robot_lab/memory.rb', line 937 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.
931 932 933 934 935 |
# File 'lib/robot_lab/memory.rb', line 931 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.
923 924 925 |
# File 'lib/robot_lab/memory.rb', line 923 def key?(key) @redis.exists?("#{@namespace}:#{key}") end |
#keys ⇒ 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.
927 928 929 |
# File 'lib/robot_lab/memory.rb', line 927 def keys @redis.keys("#{@namespace}:*").map { |k| k.sub("#{@namespace}:", "").to_sym } end |