Class: Stockpile::Redis

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

Overview

A connection manager for Redis.

Constant Summary collapse

VERSION =

:nodoc:

'1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Redis

Create a new Redis connection manager with the provided options.

Options

redis

Provides the Redis connection options.

namespace

Provides the Redis namespace to use, but only if redis-namespace is in use (detected with the existence of Redis::Namespace).

The namespace can also be provided as a key in the redis options if it is missing from the main options. If there is no namespace key present in either options or options[:redis], a namespace will be generated from one of the following: $REDIS_NAMESPACE, Rails.env (if in Rails), or $RACK_ENV.

narrow

Use a narrow connection width if true; if not provided, uses the value of ::Stockpile.narrow? in this connection manager.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/stockpile/redis.rb', line 30

def initialize(options = {})
  @redis_options = options.fetch(:redis, {})
  @narrow        = !!options.fetch(:narrow, ::Stockpile.narrow?)
  @namespace     = options.fetch(:namespace) {
    @redis_options.fetch(:namespace) {
      ENV['REDIS_NAMESPACE'] ||
      (defined?(::Rails) && ::Rails.env) ||
      ENV['RACK_ENV']
    }
  }

  if @redis_options.has_key?(:namespace)
    @redis_options = @redis_options.reject { |k, _| k == :namespace }
  end

  @connection = nil
  @clients    = {}
end

Instance Attribute Details

#connectionObject (readonly)

The current primary connection to Redis.



50
51
52
# File 'lib/stockpile/redis.rb', line 50

def connection
  @connection
end

Instance Method Details

#connect(*client_names) ⇒ Object

Connect to Redis, unless already connected. Additional client connections can be specified in the parameters as a shorthand for calls to #connection_for.

If #narrow? is true, the same Redis connection will be used for all clients managed by this connection manager.

manager.connect
manager.connection_for(:redis)

# This means the same as above.
manager.connect(:redis)


70
71
72
73
74
75
76
77
78
# File 'lib/stockpile/redis.rb', line 70

def connect(*client_names)
  @connection ||= connect_for_any

  clients_from(*client_names).each { |client_name|
    connection_for(client_name)
  }

  connection
end

#connection_for(client_name) ⇒ Object

Perform a client connection to Redis for a specific client_name. The client_name of :all will always return nil.

If the requested client does not yet exist, the connection will be created.

Because Resque depends on Redis::Namespace, #connection_for will perform special Redis::Namespace handling for a connection with the name :resque.

If #narrow? is true, the same Redis connection will be shared between all clients.

If a connection has not yet been made, it will be made.



94
95
96
97
98
99
100
101
102
103
# File 'lib/stockpile/redis.rb', line 94

def connection_for(client_name)
  connect unless connection
  return nil if client_name == :all
  @clients[client_name] ||= case client_name
                            when :resque
                              connect_for_resque
                            else
                              connect_for_any
                            end
end

#disconnect(*client_names) ⇒ Object

Disconnect from Redis for some or all clients. The primary connection will always be disconnected; other clients will be disconnected based on the clients provided. Only clients actively managed by previous calls to #connect or #connection_for will be disconnected.

If #disconnect is called with the value :all, all currently managed clients will be disconnected.

If #narrow? is true, only the primary connection will be disconnected.



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/stockpile/redis.rb', line 138

def disconnect(*client_names)
  return unless connection

  unless narrow?
    clients_from(*client_names).each { |client_name|
      redis = @clients[client_name]
      redis.quit if redis
    }
  end

  connection.quit
end

#narrow?Boolean

Indicates if this connection manager is using a narrow connection width.

Returns:

  • (Boolean)


54
55
56
# File 'lib/stockpile/redis.rb', line 54

def narrow?
  @narrow
end

#reconnect(*client_names) ⇒ Object

Reconnect to Redis for some or all clients. The primary connection will always be reconnected; other clients will be reconnected based on the clients provided. Only clients actively managed by previous calls to #connect or #connection_for will be reconnected.

If #reconnect is called with the value :all, all currently managed clients will be reconnected.

If #narrow? is true, only the primary connection will be reconnected.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/stockpile/redis.rb', line 114

def reconnect(*client_names)
  return unless connection

  connection.client.reconnect

  unless narrow?
    clients_from(*client_names).each { |client_name|
      redis = @clients[client_name]
      redis.client.reconnect if redis
    }
  end

  connection
end