]>
git.saurik.com Git - redis.git/blob - client-libraries/ruby/lib/redis.rb
3 require File
.join(File
.dirname(__FILE__
),'server')
6 class RedisError
< StandardError
8 class RedisRenameError
< StandardError
21 def initialize(opts={})
22 @opts = {:host => 'localhost
', :port => '6379'}.merge(opts)
23 $debug = @opts[:debug]
24 @server = Server.new(@opts[:host], @opts[:port])
39 def with_socket_management(server, &block)
41 block.call(server.socket)
42 #Timeout or server down
43 rescue Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNREFUSED => e
45 puts "Client (#{server.inspect}) disconnected from server: #{e.inspect}\n" if $debug
48 rescue NoMethodError => e
49 puts "Client (#{server.inspect}) tryin server that is down: #{e.inspect}\n Dying!" if $debug
59 write "SELECT
#{index}\r\n"
83 k,v = kv.split(':', 2)
84 k,v = k.chomp, v = v.chomp
94 puts "bulk_reply read value is
#{x.inspect}" if $debug
97 puts "error
in bulk_reply
#{e}" if $debug
103 with_socket_management(@server) do |socket|
104 puts "writing
: #{data}" if $debug
110 with_socket_management(@server) do |socket|
111 len = [0, len.to_i].max
112 res = socket.read(len + 2)
113 res
= res
.chomp
if res
118 def read(length
= read_proto
)
119 with_socket_management(@server) do |socket
|
120 res
= socket
.read(length
)
121 puts
"read is #{res.inspect}" if $debug
127 write
"KEYS #{glob}\r\n"
128 get_response
.split(' ')
131 def rename!
(oldkey
, newkey
)
132 write
"RENAME #{oldkey} #{newkey}\r\n"
136 def rename(oldkey
, newkey
)
137 write
"RENAMENX #{oldkey} #{newkey}\r\n"
140 raise RedisRenameError
, "source key: #{oldkey} does not exist"
142 raise RedisRenameError
, "target key: #{oldkey} already exists"
144 raise RedisRenameError
, "source and destination keys are the same"
151 write
"EXISTS #{key}\r\n"
156 write
"DEL #{key}\r\n"
165 write
"GET #{key}\r\n"
170 write
"MGET #{keys.join(' ')}\r\n"
174 def incr(key
, increment
=nil)
176 write
"INCRBY #{key} #{increment}\r\n"
178 write
"INCR #{key}\r\n"
183 def decr(key
, decrement
=nil)
185 write
"DECRRBY #{key} #{decrement}\r\n"
187 write
"DECR #{key}\r\n"
193 write
"RANDOMKEY\r\n"
198 write
"LLEN #{key}\r\n"
199 case i
= get_response
201 raise RedisError
, "key: #{key} does not hold a list value"
208 write
"TYPE #{key}\r\n"
212 def push_tail(key
, string
)
213 write
"RPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n"
217 def push_head(key
, string
)
218 write
"LPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n"
223 write
"LPOP #{key}\r\n"
228 write
"RPOP #{key}\r\n"
232 def list_set(key
, index
, val
)
233 write
"LSET #{key} #{index} #{val.to_s.size}\r\n#{val}\r\n"
238 write
"LLEN #{key}\r\n"
239 case i
= get_response
241 raise RedisError
, "key: #{key} does not hold a list value"
247 def list_range(key
, start
, ending
)
248 write
"LRANGE #{key} #{start} #{ending}\r\n"
252 def list_trim(key
, start
, ending
)
253 write
"LTRIM #{key} #{start} #{ending}\r\n"
257 def list_index(key
, index
)
258 write
"LINDEX #{key} #{index}\r\n"
262 def list_rm(key
, count
, value
)
263 write
"LREM #{key} #{count} #{value.to_s.size}\r\n#{value}\r\n"
264 case num
= get_response
266 raise RedisError
, "key: #{key} does not exist"
268 raise RedisError
, "key: #{key} does not hold a list value"
274 def set_add(key
, member
)
275 write
"SADD #{key} #{member.to_s.size}\r\n#{member}\r\n"
282 raise RedisError
, "key: #{key} contains a non set value"
286 def set_delete(key
, member
)
287 write
"SREM #{key} #{member.to_s.size}\r\n#{member}\r\n"
294 raise RedisError
, "key: #{key} contains a non set value"
299 write
"SCARD #{key}\r\n"
300 case i
= get_response
302 raise RedisError
, "key: #{key} contains a non set value"
308 def set_member
?(key
, member
)
309 write
"SISMEMBER #{key} #{member.to_s.size}\r\n#{member}\r\n"
316 raise RedisError
, "key: #{key} contains a non set value"
321 write
"SMEMBERS #{key}\r\n"
322 Set
.new(get_response
)
325 def set_intersect(*keys
)
326 write
"SINTER #{keys.join(' ')}\r\n"
327 Set
.new(get_response
)
330 def set_inter_store(destkey
, *keys
)
331 write
"SINTERSTORE #{destkey} #{keys.join(' ')}\r\n"
335 def sort(key
, opts
={})
337 cmd
<< " BY #{opts[:by]}" if opts
[:by]
338 cmd
<< " GET #{opts[:get]}" if opts
[:get]
339 cmd
<< " INCR #{opts[:incr]}" if opts
[:incr]
340 cmd
<< " DEL #{opts[:del]}" if opts
[:del]
341 cmd
<< " DECR #{opts[:decr]}" if opts
[:decr]
342 cmd
<< " #{opts[:order]}" if opts
[:order]
343 cmd
<< " LIMIT #{opts[:limit].join(' ')}" if opts
[:limit]
351 puts
"mb res is #{res.inspect}" if $debug
353 Integer(res
).times
do
355 puts
"curren vf is #{vf.inspect}" if $debug
357 puts
"current list is #{list.inspect}" if $debug
365 raise RedisError
if (r
== "\r" || r
== "\n")
377 def set(key
, val
, expiry
=nil)
378 write("SET #{key} #{val.to_s.size}\r\n#{val}\r\n")
382 def set_unless_exists(key
, val
)
383 write
"SETNX #{key} #{val.to_s.size}\r\n#{val}\r\n"
387 def status_code_reply
390 if res
.index('-') == 0
391 raise RedisError
, res
404 raise RedisError
, e
.inspect
406 puts
"reply_type is #{rtype.inspect}" if $debug
417 raise RedisError
, single_line
419 raise RedisError
, "Unknown response.."
429 while buff
[-2..-1] !
= "\r\n"
432 puts
"single_line value is #{buff[0..-3].inspect}" if $debug
437 with_socket_management(@server) do |socket
|
440 puts
"read_proto is #{x.inspect}\n\n" if $debug