Class: Redis::Client

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

Constant Summary

DEFAULTS =
{
  :url => lambda { ENV["REDIS_URL"] },
  :scheme => "redis",
  :host => "127.0.0.1",
  :port => 6379,
  :path => nil,
  :timeout => 5.0,
  :password => nil,
  :db => 0,
  :driver => nil,
  :id => nil,
  :tcp_keepalive => 0
}

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Client) initialize(options = {})

Returns a new instance of Client



66
67
68
69
70
71
72
# File 'lib/redis/client.rb', line 66

def initialize(options = {})
  @options = _parse_options(options)
  @reconnect = true
  @logger = @options[:logger]
  @connection = nil
  @command_map = {}
end

Instance Attribute Details

- (Object) command_map (readonly)

Returns the value of attribute command_map



64
65
66
# File 'lib/redis/client.rb', line 64

def command_map
  @command_map
end

- (Object) connection (readonly)

Returns the value of attribute connection



63
64
65
# File 'lib/redis/client.rb', line 63

def connection
  @connection
end

- (Object) logger

Returns the value of attribute logger



62
63
64
# File 'lib/redis/client.rb', line 62

def logger
  @logger
end

Instance Method Details

- (Object) call(command, &block)



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

def call(command, &block)
  reply = process([command]) { read }
  raise reply if reply.is_a?(CommandError)

  if block
    block.call(reply)
  else
    reply
  end
end

- (Object) call_loop(command)



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/redis/client.rb', line 106

def call_loop(command)
  error = nil

  result = without_socket_timeout do
    process([command]) do
      loop do
        reply = read
        if reply.is_a?(CommandError)
          error = reply
          break
        else
          yield reply
        end
      end
    end
  end

  # Raise error when previous block broke out of the loop.
  raise error if error

  # Result is set to the value that the provided block used to break.
  result
end

- (Object) call_pipeline(pipeline)



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/redis/client.rb', line 130

def call_pipeline(pipeline)
  with_reconnect pipeline.with_reconnect? do
    begin
      pipeline.finish(call_pipelined(pipeline.commands)).tap do
        self.db = pipeline.db if pipeline.db
      end
    rescue ConnectionError => e
      return nil if pipeline.shutdown?
      # Assume the pipeline was sent in one piece, but execution of
      # SHUTDOWN caused none of the replies for commands that were executed
      # prior to it from coming back around.
      raise e
    end
  end
end

- (Object) call_pipelined(commands)



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/redis/client.rb', line 146

def call_pipelined(commands)
  return [] if commands.empty?

  # The method #ensure_connected (called from #process) reconnects once on
  # I/O errors. To make an effort in making sure that commands are not
  # executed more than once, only allow reconnection before the first reply
  # has been read. When an error occurs after the first reply has been
  # read, retrying would re-execute the entire pipeline, thus re-issuing
  # already successfully executed commands. To circumvent this, don't retry
  # after the first reply has been read successfully.

  result = Array.new(commands.size)
  reconnect = @reconnect

  begin
    process(commands) do
      result[0] = read

      @reconnect = false

      (commands.size - 1).times do |i|
        result[i + 1] = read
      end
    end
  ensure
    @reconnect = reconnect
  end

  result
end

- (Object) call_with_timeout(command, timeout, &blk)



177
178
179
180
181
182
183
# File 'lib/redis/client.rb', line 177

def call_with_timeout(command, timeout, &blk)
  with_socket_timeout(timeout) do
    call(command, &blk)
  end
rescue ConnectionError
  retry
end

- (Object) call_without_timeout(command, &blk)



185
186
187
# File 'lib/redis/client.rb', line 185

def call_without_timeout(command, &blk)
  call_with_timeout(command, 0, &blk)
end

- (Object) connect



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/redis/client.rb', line 74

def connect
  @pid = Process.pid

  # Don't try to reconnect when the connection is fresh
  with_reconnect(false) do
    establish_connection
    call [:auth, password] if password
    call [:select, db] if db != 0
  end

  self
end

- (Boolean) connected?

Returns:

  • (Boolean)


206
207
208
# File 'lib/redis/client.rb', line 206

def connected?
  !! (connection && connection.connected?)
end

- (Object) db



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

def db
  @options[:db]
end

- (Object) db=(db)



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

def db=(db)
  @options[:db] = db.to_i
end

- (Object) disconnect



210
211
212
# File 'lib/redis/client.rb', line 210

def disconnect
  connection.disconnect if connected?
end

- (Object) driver



58
59
60
# File 'lib/redis/client.rb', line 58

def driver
  @options[:driver]
end

- (Object) host



30
31
32
# File 'lib/redis/client.rb', line 30

def host
  @options[:host]
end

- (Object) id



87
88
89
# File 'lib/redis/client.rb', line 87

def id
  @options[:id] || "redis://#{location}/#{db}"
end

- (Object) io



219
220
221
222
223
224
225
# File 'lib/redis/client.rb', line 219

def io
  yield
rescue TimeoutError
  raise TimeoutError, "Connection timed out"
rescue Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EBADF, Errno::EINVAL => e
  raise ConnectionError, "Connection lost (%s)" % [e.class.name.split("::").last]
end

- (Object) location



91
92
93
# File 'lib/redis/client.rb', line 91

def location
  path || "#{host}:#{port}"
end

- (Object) options



22
23
24
# File 'lib/redis/client.rb', line 22

def options
  Marshal.load(Marshal.dump(@options))
end

- (Object) password



46
47
48
# File 'lib/redis/client.rb', line 46

def password
  @options[:password]
end

- (Object) path



38
39
40
# File 'lib/redis/client.rb', line 38

def path
  @options[:path]
end

- (Object) port



34
35
36
# File 'lib/redis/client.rb', line 34

def port
  @options[:port]
end

- (Object) process(commands)



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/redis/client.rb', line 189

def process(commands)
  logging(commands) do
    ensure_connected do
      commands.each do |command|
        if command_map[command.first]
          command = command.dup
          command[0] = command_map[command.first]
        end

        write(command)
      end

      yield if block_given?
    end
  end
end

- (Object) read



227
228
229
230
231
# File 'lib/redis/client.rb', line 227

def read
  io do
    connection.read
  end
end

- (Object) reconnect



214
215
216
217
# File 'lib/redis/client.rb', line 214

def reconnect
  disconnect
  connect
end

- (Object) scheme



26
27
28
# File 'lib/redis/client.rb', line 26

def scheme
  @options[:scheme]
end

- (Object) timeout



42
43
44
# File 'lib/redis/client.rb', line 42

def timeout
  @options[:timeout]
end

- (Object) with_reconnect(val = true)



254
255
256
257
258
259
260
261
# File 'lib/redis/client.rb', line 254

def with_reconnect(val=true)
  begin
    original, @reconnect = @reconnect, val
    yield
  ensure
    @reconnect = original
  end
end

- (Object) with_socket_timeout(timeout)



239
240
241
242
243
244
245
246
247
248
# File 'lib/redis/client.rb', line 239

def with_socket_timeout(timeout)
  connect unless connected?

  begin
    connection.timeout = timeout
    yield
  ensure
    connection.timeout = self.timeout if connected?
  end
end

- (Object) without_reconnect(&blk)



263
264
265
# File 'lib/redis/client.rb', line 263

def without_reconnect(&blk)
  with_reconnect(false, &blk)
end

- (Object) without_socket_timeout(&blk)



250
251
252
# File 'lib/redis/client.rb', line 250

def without_socket_timeout(&blk)
  with_socket_timeout(0, &blk)
end

- (Object) write(command)



233
234
235
236
237
# File 'lib/redis/client.rb', line 233

def write(command)
  io do
    connection.write(command)
  end
end