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
12
13
# File 'lib/trophonius_redis_manager.rb', line 4

def self.connect
  if Trophonius.config.redis_connection
    if ENV['REDIS_URL'] && ENV['REDIS_URL'] != ''
      @redis ||= Redis.new(url: ENV['REDIS_URL'])
    else
      @redis ||= Redis.new
    end
  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



50
51
52
# File 'lib/trophonius_redis_manager.rb', line 50

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

.disconnectNilClass

Disconnects from redis as quickly and as silently as possible

Returns:

  • (NilClass)

    nil



58
59
60
# File 'lib/trophonius_redis_manager.rb', line 58

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



30
31
32
33
# File 'lib/trophonius_redis_manager.rb', line 30

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



20
21
22
23
# File 'lib/trophonius_redis_manager.rb', line 20

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



41
42
43
44
# File 'lib/trophonius_redis_manager.rb', line 41

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