Class: Redis::Client

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

Direct Known Subclasses

ThreadSafe

Defined Under Namespace

Classes: ThreadSafe

Constant Summary collapse

MINUS =
"-".freeze
PLUS =
"+".freeze
COLON =
":".freeze
DOLLAR =
"$".freeze
ASTERISK =
"*".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
# File 'lib/redis/client.rb', line 12

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]
  @sock = nil
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



9
10
11
# File 'lib/redis/client.rb', line 9

def db
  @db
end

#hostObject

Returns the value of attribute host.



9
10
11
# File 'lib/redis/client.rb', line 9

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/redis/client.rb', line 9

def logger
  @logger
end

#passwordObject

Returns the value of attribute password.



9
10
11
# File 'lib/redis/client.rb', line 9

def password
  @password
end

#portObject

Returns the value of attribute port.



9
10
11
# File 'lib/redis/client.rb', line 9

def port
  @port
end

#timeoutObject

Returns the value of attribute timeout.



10
11
12
# File 'lib/redis/client.rb', line 10

def timeout
  @timeout
end

Instance Method Details

#call(*args) ⇒ Object



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

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

#call_loop(*args) ⇒ Object



39
40
41
42
43
# File 'lib/redis/client.rb', line 39

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

#call_pipelined(commands) ⇒ Object



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

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

#call_without_timeout(*args) ⇒ Object



51
52
53
54
55
# File 'lib/redis/client.rb', line 51

def call_without_timeout(*args)
  without_socket_timeout do
    call(*args)
  end
end

#connectObject



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

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

#connected?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/redis/client.rb', line 66

def connected?
  !! @sock
end

#disconnectObject



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

def disconnect
  return unless connected?

  begin
    @sock.close
  rescue
  ensure
    @sock = nil
  end
end

#idObject



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

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

#process(*commands) ⇒ Object



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

def process(*commands)
  logging(commands) do
    ensure_connected do
      @sock.write(join_commands(commands))
      yield if block_given?
    end
  end
end

#readObject

Raises:

  • (Errno::ECONNRESET)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/redis/client.rb', line 86

def read

  # We read the first byte using read() mainly because gets() is
  # immune to raw socket timeouts.
  begin
    reply_type = @sock.read(1)
  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"
  end

  raise Errno::ECONNRESET, "Connection lost" unless reply_type

  format_reply(reply_type, @sock.gets)
end

#reconnectObject



81
82
83
84
# File 'lib/redis/client.rb', line 81

def reconnect
  disconnect
  connect
end

#without_socket_timeoutObject



107
108
109
110
111
112
113
114
115
116
# File 'lib/redis/client.rb', line 107

def without_socket_timeout
  ensure_connected do
    begin
      self.timeout = 0
      yield
    ensure
      self.timeout = @timeout
    end
  end
end