- def get(key)
- execute_command("GET #{key}\r\n")
- end
-
- def mget(*keys)
- execute_command("MGET #{keys.join(' ')}\r\n")
- end
-
- def incr(key, increment=nil)
- if increment
- execute_command("INCRBY #{key} #{increment}\r\n")
- else
- execute_command("INCR #{key}\r\n")
- end
- end
-
- def decr(key, decrement=nil)
- if decrement
- execute_command("DECRBY #{key} #{decrement}\r\n")
- else
- execute_command("DECR #{key}\r\n")
- end
- end
-
- def randkey
- execute_command("RANDOMKEY\r\n")
- end
-
- def list_length(key)
- case i = execute_command("LLEN #{key}\r\n")
- when -2
- raise RedisError, "key: #{key} does not hold a list value"
- else
- i
+ ReplyProcessor = {
+ "exists" => ConvertToBool,
+ "sismember"=> ConvertToBool,
+ "sadd"=> ConvertToBool,
+ "srem"=> ConvertToBool,
+ "smove"=> ConvertToBool,
+ "move"=> ConvertToBool,
+ "setnx"=> ConvertToBool,
+ "del"=> ConvertToBool,
+ "renamenx"=> ConvertToBool,
+ "expire"=> ConvertToBool,
+ "keys" => lambda{|r| r.split(" ")},
+ "info" => lambda{|r|
+ info = {}
+ r.each_line {|kv|
+ k,v = kv.split(":",2).map{|x| x.chomp}
+ info[k.to_sym] = v
+ }
+ info
+ }
+ }
+
+ Aliases = {
+ "flush_db" => "flushdb",
+ "flush_all" => "flushall",
+ "last_save" => "lastsave",
+ "key?" => "exists",
+ "delete" => "del",
+ "randkey" => "randomkey",
+ "list_length" => "llen",
+ "push_tail" => "rpush",
+ "push_head" => "lpush",
+ "pop_tail" => "rpop",
+ "pop_head" => "lpop",
+ "list_set" => "lset",
+ "list_range" => "lrange",
+ "list_trim" => "ltrim",
+ "list_index" => "lindex",
+ "list_rm" => "lrem",
+ "set_add" => "sadd",
+ "set_delete" => "srem",
+ "set_count" => "scard",
+ "set_member?" => "sismember",
+ "set_members" => "smembers",
+ "set_intersect" => "sinter",
+ "set_intersect_store" => "sinterstore",
+ "set_inter_store" => "sinterstore",
+ "set_union" => "sunion",
+ "set_union_store" => "sunionstore",
+ "set_diff" => "sdiff",
+ "set_diff_store" => "sdiffstore",
+ "set_move" => "smove",
+ "set_unless_exists" => "setnx",
+ "rename_unless_exists" => "renamenx",
+ "type?" => "type"
+ }
+
+ def initialize(opts={})
+ @host = opts[:host] || '127.0.0.1'
+ @port = opts[:port] || 6379
+ @db = opts[:db] || 0
+ @timeout = opts[:timeout] || 5
+ $debug = opts[:debug] || false
+ connect_to_server