# @deprecated The preferred way to create a new client object is using `#new`.
# This method does not actually establish a connection to Redis,
# in contrary to what you might expect.
defself.connect(options={})
new(options)
end
defself.current
@current||=Redis.new
end
defself.current=(redis)
@current=redis
end
includeMonitorMixin
# Create a new client instance
#
# @param [Hash] options
# @option options [String] :url (value of the environment variable REDIS_URL) a Redis URL, for a TCP connection: `redis://:[password]@[hostname]:[port]/[db]` (password, port and database are optional), for a unix socket connection: `unix://[path to Redis socket]`. This overrides all other options.
# @option options [String] :host ("127.0.0.1") server hostname
# @option options [Fixnum] :port (6379) server port
# @option options [String] :path path to server socket (overrides host and port)
# @option options [Float] :timeout (5.0) timeout in seconds
# @option options [Float] :connect_timeout (same as timeout) timeout for initial connect in seconds
# @option options [String] :password Password to authenticate against server
# @option options [Fixnum] :db (0) Database to select after initial connect
# @option options [Symbol] :driver Driver to use, currently supported: `:ruby`, `:hiredis`, `:synchrony`
# @option options [String] :id ID for the client connection, assigns name to current connection by sending `CLIENT SETNAME`
# @option options [Hash, Fixnum] :tcp_keepalive Keepalive values, if Fixnum `intvl` and `probe` are calculated based on the value, if Hash `time`, `intvl` and `probes` can be specified as a Fixnum
# @option options [Fixnum] :reconnect_attempts Number of attempts trying to connect
# @option options [Boolean] :inherit_socket (false) Whether to use socket in forked process or not
# @option options [Array] :sentinels List of sentinels to contact
# @option options [Symbol] :role (:master) Role to fetch via Sentinel, either `:master` or `:slave`
#
# @return [Redis] a new client instance
definitialize(options={})
@options=options.dup
@original_client=@client=Client.new(options)
@queue=Hash.new{|h,k|h[k]=[]}
super()# Monitor#initialize
end
defsynchronize
mon_synchronize{yield(@client)}
end
# Run code with the client reconnecting
defwith_reconnect(val=true,&blk)
synchronizedo|client|
client.with_reconnect(val,&blk)
end
end
# Run code without the client reconnecting
defwithout_reconnect(&blk)
with_reconnect(false,&blk)
end
# Test whether or not the client is connected
defconnected?
@original_client.connected?
end
# Disconnect the client as quickly and silently as possible.
defclose
@original_client.disconnect
end
aliasdisconnect!close
# Sends a command to Redis and returns its reply.
#
# Replies are converted to Ruby objects according to the RESP protocol, so
# you can expect a Ruby array, integer or nil when Redis sends one. Higher
# level transformations, such as converting an array of pairs into a Ruby
# hash, are up to consumers.
#
# Redis error replies are raised as Ruby exceptions.
defcall(*command)
synchronizedo|client|
client.call(command)
end
end
# Queues a command for pipelining.
#
# Commands in the queue are executed with the Redis#commit method.
#
# See http://redis.io/topics/pipelining for more details.
#
defqueue(*command)
@queue[Thread.current.object_id]<<command
end
# Sends all commands in the queue.
#
# See http://redis.io/topics/pipelining for more details.
"#{@command.to_s.upcase} cannot be used in Redis::Distributed because the keys involved need to be on the same server or because we cannot guarantee that the operation will be atomic."