"smove" => true
}
- BOOLEAN_PROCESSOR = lambda{|r| r == 0 ? false : r}
+ BOOLEAN_PROCESSOR = lambda{|r| r == 1 }
REPLY_PROCESSOR = {
"exists" => BOOLEAN_PROCESSOR,
"type?" => "type"
}
+ DISABLED_COMMANDS = {
+ "monitor" => true,
+ "sync" => true
+ }
+
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
- $debug = options[:debug]
+ @password = options[:password]
+ @logger = options[:logger]
+
+ @logger.info { self.to_s } if @logger
connect_to_server
end
def to_s
- "Redis Client connected to #{@host}:#{@port} against DB #{@db}"
+ "Redis Client connected to #{server} against DB #{@db}"
+ end
+
+ def server
+ "#{@host}:#{@port}"
end
def connect_to_server
@sock = connect_to(@host, @port, @timeout == 0 ? nil : @timeout)
+ call_command(["auth",@password]) if @password
call_command(["select",@db]) unless @db == 0
end
end
def call_command(argv)
- puts argv.inspect if $debug
+ @logger.debug { argv.inspect } if @logger
+
# this wrapper to raw_call_command handle reconnection on socket
# error. We try to reconnect just one time, otherwise let the error
# araise.
connect_to_server if !@sock
+
begin
raw_call_command(argv.dup)
rescue Errno::ECONNRESET, Errno::EPIPE
@sock.close
+ @sock = nil
connect_to_server
raw_call_command(argv.dup)
end
bulk = nil
argv[0] = argv[0].to_s.downcase
argv[0] = ALIASES[argv[0]] if ALIASES[argv[0]]
+ raise "#{argv[0]} command is disabled" if DISABLED_COMMANDS[argv[0]]
if BULK_COMMANDS[argv[0]] and argv.length > 1
bulk = argv[-1].to_s
- argv[-1] = bulk.length
+ argv[-1] = bulk.respond_to?(:bytesize) ? bulk.bytesize : bulk.size
end
- command << argv.join(' ') + "\r\n"
- command << bulk + "\r\n" if bulk
+ command << "#{argv.join(' ')}\r\n"
+ command << "#{bulk}\r\n" if bulk
end
@sock.write(command)
end
def [](key)
- get(key)
+ self.get(key)
end
def []=(key,value)
end
def sort(key, options = {})
- cmd = []
- cmd << "SORT #{key}"
+ cmd = ["SORT"]
+ cmd << key
cmd << "BY #{options[:by]}" if options[:by]
cmd << "GET #{[options[:get]].flatten * ' GET '}" if options[:get]
cmd << "#{options[:order]}" if options[:order]
call_command(decrement ? ["decrby",key,decrement] : ["decr",key])
end
+ # Similar to memcache.rb's #get_multi, returns a hash mapping
+ # keys to values.
+ def mapped_mget(*keys)
+ mget(*keys).inject({}) do |hash, value|
+ key = keys.shift
+ value.nil? ? hash : hash.merge(key => value)
+ end
+ end
+
# Ruby defines a now deprecated type method so we need to override it here
# since it will never hit method_missing
def type(key)