Module: SolidCache::Connections

Defined in:
lib/solid_cache/connections.rb,
lib/solid_cache/connections/single.rb,
lib/solid_cache/connections/sharded.rb,
lib/solid_cache/connections/unmanaged.rb

Defined Under Namespace

Classes: Sharded, Single, Unmanaged

Class Method Summary collapse

Class Method Details

.from_config(options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/solid_cache/connections.rb', line 5

def self.from_config(options)
  if options.present? || SolidCache.all_shards_config.present?
    case options
    when NilClass
      names = SolidCache.all_shard_keys
      nodes = names.to_h { |name| [ name, name ] }
    when Array
      names = options
      nodes = names.to_h { |name| [ name, name ] }
    when Hash
      names = options.keys
      nodes = options.invert
    end

    if (unknown_shards = names - SolidCache.all_shard_keys).any?
      raise ArgumentError, "Unknown #{"shard".pluralize(unknown_shards)}: #{unknown_shards.join(", ")}"
    end

    if names.size == 1
      Single.new(names.first)
    else
      Sharded.new(names, nodes)
    end
  else
    Unmanaged.new
  end
end