Class: Redis::Client

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

Direct Known Subclasses

ThreadSafe

Defined Under Namespace

Classes: ThreadSafe

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
# File 'lib/redis/client.rb', line 7

def initialize(options = {})
  @host = options[:host] || "127.0.0.1"
  @port = (options[:port] || 6379).to_i
  @db = (options[:db] || 0).to_i
  @timeout = (options[:timeout] || 5).to_i
  @password = options[:password]
  @logger = options[:logger]
  @connection = Connection.new
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/redis/client.rb', line 5

def connection
  @connection
end

#dbObject

Returns the value of attribute db.



3
4
5
# File 'lib/redis/client.rb', line 3

def db
  @db
end

#hostObject

Returns the value of attribute host.



3
4
5
# File 'lib/redis/client.rb', line 3

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'lib/redis/client.rb', line 3

def logger
  @logger
end

#passwordObject

Returns the value of attribute password.



3
4
5
# File 'lib/redis/client.rb', line 3

def password
  @password
end

#portObject

Returns the value of attribute port.



3
4
5
# File 'lib/redis/client.rb', line 3

def port
  @port
end

#timeoutObject

Returns the value of attribute timeout.



4
5
6
# File 'lib/redis/client.rb', line 4

def timeout
  @timeout
end

Instance Method Details

#call(*args) ⇒ Object



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

def call(*args)
  process(args) do
    read
  end
end

#call_loop(*args) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/redis/client.rb', line 34

def call_loop(*args)
  without_socket_timeout do
    process(args) do
      loop { yield(read) }
    end
  end
end

#call_pipelined(commands) ⇒ Object



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

def call_pipelined(commands)
  process(*commands) do
    Array.new(commands.size) { read }
  end
end

#call_without_timeout(*args) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/redis/client.rb', line 48

def call_without_timeout(*args)
  without_socket_timeout do
    call(*args)
  end
rescue Errno::ECONNRESET
  retry
end

#connectObject



17
18
19
20
21
22
# File 'lib/redis/client.rb', line 17

def connect
  connect_to(@host, @port)
  call(:auth, @password) if @password
  call(:select, @db) if @db != 0
  self
end

#connected?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/redis/client.rb', line 68

def connected?
  connection.connected?
end

#disconnectObject



72
73
74
# File 'lib/redis/client.rb', line 72

def disconnect
  connection.disconnect if connection.connected?
end

#idObject



24
25
26
# File 'lib/redis/client.rb', line 24

def id
  "redis://#{host}:#{port}/#{db}"
end

#process(*commands) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/redis/client.rb', line 56

def process(*commands)
  logging(commands) do
    ensure_connected do
      commands.each do |command|
        connection.write(command)
      end

      yield if block_given?
    end
  end
end

#readObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/redis/client.rb', line 81

def read
  begin
    connection.read

  rescue Errno::EAGAIN
    # We want to make sure it reconnects on the next command after the
    # timeout. Otherwise the server may reply in the meantime leaving
    # the protocol in a desync status.
    disconnect

    raise Errno::EAGAIN, "Timeout reading from the socket"

  rescue Errno::ECONNRESET
    raise Errno::ECONNRESET, "Connection lost"
  end
end

#reconnectObject



76
77
78
79
# File 'lib/redis/client.rb', line 76

def reconnect
  disconnect
  connect
end

#without_socket_timeoutObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/redis/client.rb', line 98

def without_socket_timeout
  connect unless connected?

  begin
    self.timeout = 0
    yield
  ensure
    self.timeout = @timeout if connected?
  end
end