Class: Redis::Factory

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

Class Method Summary collapse

Class Method Details

.convert_to_redis_client_options(address_or_options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/redis/factory.rb', line 15

def self.convert_to_redis_client_options(address_or_options)
  if address_or_options.is_a?(Hash)
    options = address_or_options.dup
    options[:namespace] ||= options.delete(:key_prefix) # RailsSessionStore
    options
  else
    if address_or_options =~ /redis\:\/\//
      require 'uri'
      uri = URI.parse address_or_options
      _, db, namespace = if uri.path
        uri.path.split /\//
      end
    else
      warn "[DEPRECATION] `#{address_or_options}` is deprecated. Please use `redis://#{address_or_options}` instead."
      address_or_options, password = address_or_options.split(/\@/).reverse
      password = password.gsub(/\:/, "") if password
      host, port = address_or_options.split /\:/
      port, db, namespace = port.split /\// if port
    end

    options = {}
    options[:host] = host || uri && uri.host
    options[:port] = port || uri && uri.port
    options[:db]  = db.to_i if db
    options[:namespace] = namespace if namespace
    options[:password]  = password || uri && uri.password
    options[:marshalling] = false if RUBY_VERSION =~ /1\.9/
    options
  end
end

.create(*redis_client_options) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/redis/factory.rb', line 3

def self.create(*redis_client_options)
  redis_client_options = redis_client_options.flatten.compact.inject([]) do |result, address|
    result << convert_to_redis_client_options(address)
    result
  end
  if redis_client_options.size > 1
    ::Redis::DistributedStore.new redis_client_options
  else
    ::Redis::Store.new redis_client_options.first || {}
  end
end