lib/redis.rb in redis-2.0.0.rc2 vs lib/redis.rb in redis-2.0.0.rc3
- old
+ new
@@ -1,9 +1,9 @@
require 'socket'
class Redis
- VERSION = "2.0.0.rc2"
+ VERSION = "2.0.0.rc3"
class ProtocolError < RuntimeError
def initialize(reply_type)
super("Protocol error, got '#{reply_type}' as initial reply byte")
end
@@ -27,11 +27,15 @@
new(options)
end
def initialize(options = {})
- @client = Client.new(options)
+ if options[:thread_safe]
+ @client = Client::ThreadSafe.new(options)
+ else
+ @client = Client.new(options)
+ end
end
def select(db)
@client.db = db
@client.call(:select, db)
@@ -227,38 +231,59 @@
def zcard(key)
@client.call(:zcard, key)
end
- def zrange(key, start, stop, with_scores = false)
- if with_scores
- @client.call(:zrange, key, start, stop, "WITHSCORES")
- else
- @client.call(:zrange, key, start, stop)
+ def zrange(key, start, stop, options = {})
+ command = CommandOptions.new(options) do |c|
+ c.bool :with_scores
end
+
+ @client.call(:zrange, key, start, stop, *command.to_a)
end
- def zrangebyscore(key, min, max)
- @client.call(:zrangebyscore, key, min, max)
+ def zrangebyscore(key, min, max, options = {})
+ command = CommandOptions.new(options) do |c|
+ c.splat :limit
+ c.bool :with_scores
+ end
+
+ @client.call(:zrangebyscore, key, min, max, *command.to_a)
end
- def zrevrange(key, start, stop, with_scores = false)
- if with_scores
- @client.call(:zrevrange, key, start, stop, "WITHSCORES")
- else
- @client.call(:zrevrange, key, start, stop)
+ def zrevrange(key, start, stop, options = {})
+ command = CommandOptions.new(options) do |c|
+ c.bool :with_scores
end
+
+ @client.call(:zrevrange, key, start, stop, *command.to_a)
end
+ def zremrangebyscore(key, min, max)
+ @client.call(:zremrangebyscore, key, min, max)
+ end
+
+ def zremrangebyrank(key, start, stop)
+ @client.call(:zremrangebyscore, key, start, stop)
+ end
+
def zscore(key, member)
@client.call(:zscore, key, member)
end
def zrem(key, member)
_bool @client.call(:zrem, key, member)
end
+ def zinter(destination, keys)
+ @client.call(:zinter, destination, keys.size, *keys)
+ end
+
+ def zunion(destination, keys)
+ @client.call(:zunion, destination, keys.size, *keys)
+ end
+
def move(key, db)
_bool @client.call(:move, key, db)
end
def setnx(key, value)
@@ -357,24 +382,19 @@
end
result
end
def sort(key, options = {})
- cmd = []
- cmd << "SORT"
- cmd << key
- cmd += ["BY", options[:by]] if options[:by]
+ command = CommandOptions.new(options) do |c|
+ c.value :by
+ c.splat :limit
+ c.multi :get
+ c.words :order
+ c.value :store
+ end
- Array(options[:get]).each do |k|
- cmd += ["GET", k]
- end if options[:get]
-
- cmd += options[:order].split(" ") if options[:order]
- cmd += ["LIMIT", *options[:limit]] if options[:limit]
- cmd += ["STORE", options[:store]] if options[:store]
-
- @client.call(*cmd)
+ @client.call(:sort, key, *command.to_a)
end
def incr(key)
@client.call(:incr, key)
end
@@ -458,10 +478,46 @@
def method_missing(command, *args)
@client.call(command, *args)
end
+ class CommandOptions
+ def initialize(options)
+ @result = []
+ @options = options
+ yield(self)
+ end
+
+ def bool(name)
+ insert(name) { |argument, value| [argument] }
+ end
+
+ def value(name)
+ insert(name) { |argument, value| [argument, value] }
+ end
+
+ def splat(name)
+ insert(name) { |argument, value| [argument, *value] }
+ end
+
+ def multi(name)
+ insert(name) { |argument, value| [argument].product(Array(value)).flatten }
+ end
+
+ def words(name)
+ insert(name) { |argument, value| value.split(" ") }
+ end
+
+ def to_a
+ @result
+ end
+
+ def insert(name)
+ @result += yield(name.to_s.upcase.gsub("_", ""), @options[name]) if @options[name]
+ end
+ end
+
private
def _bool(value)
value == 1
end
@@ -492,5 +548,6 @@
end
require 'redis/client'
require 'redis/pipeline'
require 'redis/subscribe'
+require 'redis/compat'