Class: Routemaster::RedisBroker

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/routemaster/redis_broker.rb

Constant Summary collapse

DEFAULT_NAMESPACE =
'rm'.freeze

Instance Method Summary collapse

Constructor Details

#initializeRedisBroker

Returns a new instance of RedisBroker.



12
13
14
15
# File 'lib/routemaster/redis_broker.rb', line 12

def initialize
  @_connections = {}
  _cleanup
end

Instance Method Details

#cleanupObject



26
27
28
# File 'lib/routemaster/redis_broker.rb', line 26

def cleanup
  _cleanup
end

#get(name, urls: []) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/routemaster/redis_broker.rb', line 17

def get(name, urls: [])
  _check_for_fork
  @_connections[name] ||= begin
                            parsed_url = URI.parse(urls.first)
                            namespace = parsed_url.path.split('/')[2] || DEFAULT_NAMESPACE
                            Redis::Namespace.new(namespace, redis: Redis::Distributed.new(urls))
                          end
end

#inject(clients = {}) ⇒ Object

Allow to inject pre-built Redis clients

Before storing a new connection, ensures that any previously set client is properly closed.



35
36
37
38
39
40
41
# File 'lib/routemaster/redis_broker.rb', line 35

def inject(clients={})
  @_injected_clients = true
  clients.each_pair do |name, client|
    _close_if_present(@_connections[name])
    @_connections[name] = Redis::Namespace.new(DEFAULT_NAMESPACE, redis: client)
  end
end