lib/rediska/driver.rb in rediska-0.2.7 vs lib/rediska/driver.rb in rediska-0.3.0
- old
+ new
@@ -23,13 +23,10 @@
def read
replies.shift
end
# NOT IMPLEMENTED:
- # * blpop
- # * brpop
- # * brpoplpush
# * subscribe
# * psubscribe
# * publish
def flushdb
@@ -299,34 +296,42 @@
remove_key_for_empty_collection(key)
diff
end
def rpush(key, value)
+ raise_argument_error('rpush') if value.respond_to?(:each) && value.empty?
+
data_type_check(key, Array)
data[key] ||= []
[value].flatten.each do |val|
data[key].push(val.to_s)
end
data[key].size
end
def rpushx(key, value)
+ raise_argument_error('rpushx') if value.respond_to?(:each) && value.empty?
+
data_type_check(key, Array)
return unless data[key]
rpush(key, value)
end
def lpush(key, value)
+ raise_argument_error('lpush') if value.respond_to?(:each) && value.empty?
+
data_type_check(key, Array)
data[key] ||= []
[value].flatten.each do |val|
data[key].unshift(val.to_s)
end
data[key].size
end
def lpushx(key, value)
+ raise_argument_error('lpushx') if value.respond_to?(:each) && value.empty?
+
data_type_check(key, Array)
return unless data[key]
lpush(key, value)
end
@@ -334,20 +339,49 @@
data_type_check(key, Array)
return unless data[key]
data[key].pop
end
+ def brpop(keys, timeout = 0)
+ keys = Array(keys)
+ keys.each do |key|
+ if data[key] && data[key].size > 0
+ return [key, data[key].pop]
+ end
+ end
+ sleep(timeout.to_f)
+ nil
+ end
+
def rpoplpush(key1, key2)
data_type_check(key1, Array)
rpop(key1).tap do |elem|
lpush(key2, elem) unless elem.nil?
end
end
+ def brpoplpush(key1, key2, opts = {})
+ data_type_check(key1, Array)
+ brpop(key1).tap do |elem|
+ lpush(key2, elem) unless elem.nil?
+ end
+ end
+
def lpop(key)
data_type_check(key, Array)
return unless data[key]
data[key].shift
+ end
+
+ def blpop(keys, timeout = 0)
+ keys = Array(keys)
+ keys.each do |key|
+ if data[key] && data[key].size > 0
+ return [key, data[key].shift]
+ end
+ end
+ sleep(timeout.to_f)
+ nil
end
def smembers(key)
data_type_check(key, ::Set)
return [] unless data[key]