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

Class Method Details

.connectObject



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

Returns:

  • (Boolean)

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



48
49
50
# File 'lib/trophonius_redis_manager.rb', line 48

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

.disconnectNilClass

Disconnects from redis as quickly and as silently as possible

Returns:

  • (NilClass)

    nil



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

Parameters:

  • key: (String)

    the key to find

Returns:

  • (String)

    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

Parameters:

  • key: (String)

    the key to check

Returns:

  • (Boolean)

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



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

Parameters:

  • key: (String)

    the key to store in redis

  • value: (any)

    the value for the key

Returns:

  • (String)

    the value corresponding with the 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