Module: Trophonius::RedisManager

Defined in:
lib/connectors/redis_manager.rb

Overview

the RedisManager module is used to create a (single) connection to a redis store.

Class Method Summary collapse

Class Method Details

.connectObject



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/connectors/redis_manager.rb', line 4

def self.connect
  return unless Trophonius.config.redis_connection

  redis_url = ENV.fetch('REDIS_URL')
  options = {}
  options.merge!(url: redis_url) if redis_url && redis_url != ''
  options.merge!(ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE }) if Trophonius.config.redis_no_verify
  @redis ||= Redis.new(options)

  nil
end

.connected?Boolean

Checks whether we are connected to redis

Returns:

  • (Boolean)

    true or false depending on whether a connection to redis has been established



51
52
53
# File 'lib/connectors/redis_manager.rb', line 51

def self.connected?
  @redis.nil? == false && @redis.connected?
end

.disconnectNilClass

Disconnects from redis as quickly and as silently as possible

Returns:

  • (NilClass)

    nil



59
60
61
# File 'lib/connectors/redis_manager.rb', line 59

def self.disconnect
  @redis.disconnect!
end

.get_key(key:) ⇒ String

Get the value corresponding with the key

Parameters:

  • key: (String)

    the key to find

Returns:

  • (String)

    the value corresponding with the key



31
32
33
34
# File 'lib/connectors/redis_manager.rb', line 31

def self.get_key(key:)
  connect unless connected?
  @redis.get(key)
end

.key_exists?(key:) ⇒ Boolean

Checks whether the given key exists

Parameters:

  • key: (String)

    the key to check

Returns:

  • (Boolean)

    true or false depending on whether the key exists in the redis store or not



21
22
23
24
# File 'lib/connectors/redis_manager.rb', line 21

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

Parameters:

  • key: (String)

    the key to store in redis

  • value: (any)

    the value for the key

Returns:

  • (String)

    the value corresponding with the key



42
43
44
45
# File 'lib/connectors/redis_manager.rb', line 42

def self.set_key(key:, value:)
  connect unless connected?
  @redis.set(key, value)
end