Class: RedisConnectionFixer
- Inherits:
-
Object
- Object
- RedisConnectionFixer
- 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
-
.find_namespace(key) ⇒ String?
Determines the appropriate namespace based on the environment key.
-
.fix(url) ⇒ Array<String, Symbol>
Fixes a Redis URL to ensure it has the correct format and returns the appropriate driver.
-
.wrap_redis(redis_instance) ⇒ Redis, Redis::Namespace
Wraps a Redis instance with a namespace if appropriate for the current environment.
Instance Method Summary collapse
-
#decide_driver(url) ⇒ Symbol
Determines the appropriate Redis driver based on the URL and environment.
-
#fix(url) ⇒ Array<String, Symbol>
Fixes a Redis URL and determines the appropriate driver.
-
#fix_redis_url(url) ⇒ String
Ensures that a Redis URL has the redis:// or rediss:// prefix.
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.
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.
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.
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.
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.
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.
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 |