Module: Trophonius::RedisManager
- Defined in:
- lib/trophonius_redis_manager.rb
Overview
the RedisManager module is used to create a (single) connection to a redis store.
Class Method Summary collapse
- .connect ⇒ Object
-
.connected? ⇒ Boolean
Checks whether we are connected to redis.
-
.disconnect ⇒ NilClass
Disconnects from redis as quickly and as silently as possible.
-
.get_key(key:) ⇒ String
Get the value corresponding with the key.
-
.key_exists?(key:) ⇒ Boolean
Checks whether the given key exists.
-
.set_key(key:, value:) ⇒ String
Set the value corresponding with a key.
Class Method Details
.connect ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/trophonius_redis_manager.rb', line 4 def self.connect if ENV['REDIS_URL'] && ENV['REDIS_URL'] != '' @redis ||= Redis.new(url: ENV['REDIS_URL']) else @redis ||= Redis.new end return nil end |
.connected? ⇒ Boolean
Checks whether we are connected to redis
48 49 50 |
# File 'lib/trophonius_redis_manager.rb', line 48 def self.connected? @redis.nil? == false && @redis.connected? end |
.disconnect ⇒ NilClass
Disconnects from redis as quickly and as silently as possible
56 57 58 |
# File 'lib/trophonius_redis_manager.rb', line 56 def self.disconnect @redis.disconnect! end |
.get_key(key:) ⇒ String
Get the value corresponding with the key
28 29 30 31 |
# File 'lib/trophonius_redis_manager.rb', line 28 def self.get_key(key:) connect unless connected? @redis.get(key) end |
.key_exists?(key:) ⇒ Boolean
Checks whether the given key exists
18 19 20 21 |
# File 'lib/trophonius_redis_manager.rb', line 18 def self.key_exists?(key:) connect unless connected? !(@redis.get(key).nil? || @redis.get(key).empty?) end |
.set_key(key:, value:) ⇒ String
Set the value corresponding with a key
39 40 41 42 |
# File 'lib/trophonius_redis_manager.rb', line 39 def self.set_key(key:, value:) connect unless connected? @redis.set(key, value) end |