Class: RedisConnectionFixer

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_connection_fixer.rb

Overview

RedisConnectionFixer handles Redis connection string formatting and namespace configuration. This class ensures that Redis URLs are properly formatted and that environments have appropriate namespaces.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_namespace(key) ⇒ String?

Determines the appropriate namespace based on the environment key. This is used to isolate Redis keys in shared Redis instances.

Examples:

RedisConnectionFixer.find_namespace('development') #=> 'development'
RedisConnectionFixer.find_namespace(ENV['RAILS_ENV_REAL']) #=> ENV['NAMESPACE']
RedisConnectionFixer.find_namespace(nil) #=> nil

Parameters:

  • key (String, Symbol, nil)

    The environment key to find the namespace for.

Returns:

  • (String, nil)

    The namespace to use, or nil if no namespace should be used.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/redis_connection_fixer.rb', line 23

def self.find_namespace(key)
  return nil if key.blank?

  case key.to_s.to_sym
  when :development
    'development'
  when :staging
    ENV['NAMESPACE'].presence || raise('NAMESPACE not set')
  else
    ''
  end
end

.fix(url) ⇒ Array<String, Symbol>

Fixes a Redis URL to ensure it has the correct format and returns the appropriate driver.

Examples:

RedisConnectionFixer.fix('localhost:6379/0') #=> ['redis://localhost:6379/0', :ruby]

Parameters:

  • url (String)

    The Redis URL to fix, which may or may not include the redis:// prefix.

Returns:

  • (Array<String, Symbol>)

    An array containing the fixed URL and the driver to use.



10
11
12
# File 'lib/redis_connection_fixer.rb', line 10

def self.fix(url)
  new.fix(url)
end

.wrap_redis(redis_instance) ⇒ Redis, Redis::Namespace

Wraps a Redis instance with a namespace if appropriate for the current environment. This prevents key conflicts in shared Redis instances.

Parameters:

  • redis_instance (Redis)

    The Redis instance to wrap.

Returns:

  • (Redis, Redis::Namespace)

    The original Redis instance or a namespaced version.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/redis_connection_fixer.rb', line 41

def self.wrap_redis(redis_instance)
  if ENV['RAILS_ENV_REAL'].to_s == 'staging'
    namespace = find_namespace(ENV['RAILS_ENV_REAL'])
    Redis::Namespace.new(namespace, redis: redis_instance)
  elsif Rails.env.development?
    namespace = find_namespace(:development)
    Redis::Namespace.new(namespace, redis: redis_instance)
  else
    redis_instance
  end
end

Instance Method Details

#decide_driver(url) ⇒ Symbol

Determines the appropriate Redis driver based on the URL and environment.

Parameters:

  • url (String)

    The Redis URL.

Returns:

  • (Symbol)

    The driver to use (:hiredis or :ruby).



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/redis_connection_fixer.rb', line 80

def decide_driver(url)
  if url.include?('rediss')
    :ruby
  elsif ENV['RAILS_ENV_REAL'] == 'staging'
    :ruby
  elsif defined?(Redis::Connection::Hiredis)
    :hiredis
  else
    :ruby
  end
end

#fix(url) ⇒ Array<String, Symbol>

Fixes a Redis URL and determines the appropriate driver.

Parameters:

  • url (String)

    The Redis URL to fix.

Returns:

  • (Array<String, Symbol>)

    An array containing the fixed URL and the driver to use.



57
58
59
60
61
62
# File 'lib/redis_connection_fixer.rb', line 57

def fix(url)
  url = url.dup.to_s
  redis_url = fix_redis_url(url)
  driver = decide_driver(redis_url)
  [redis_url, driver]
end

#fix_redis_url(url) ⇒ String

Ensures that a Redis URL has the redis:// or rediss:// prefix.

Parameters:

  • url (String)

    The Redis URL to fix.

Returns:

  • (String)

    The fixed Redis URL.



68
69
70
71
72
73
74
# File 'lib/redis_connection_fixer.rb', line 68

def fix_redis_url(url)
  ['redis:', 'rediss:'].each do |scheme|
    return url if url.include?(scheme)
  end

  "redis://#{url}"
end