Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add #[] to ItemEnumerator

I.e. allow random access. (The usual caveats apply.)
  • Loading branch information...
commit 81990b3c904ee2fc3ad557966256d3b4262876c2 1 parent a9e1e9a
@wmorgan authored
Showing with 13 additions and 8 deletions.
  1. +13 −8 lib/redis-scheduler.rb
View
21 lib/redis-scheduler.rb
@@ -124,21 +124,26 @@ def initialize redis, q
@q = q
end
- BLOCK_SIZE = 10
+ PAGE_SIZE = 50
def each
start = 0
while start < size
- elements = @redis.zrange @q, start, start + BLOCK_SIZE,
- :withscores => true
- elements.each_slice(2) do |item, at| # isgh
- item =~ /^\d+:(\S+)$/ or raise InvalidEntryException, item
- item = $1
- yield item, Time.at(at.to_f)
- end
+ elements = self[start, PAGE_SIZE]
+ elements.each { |*x| yield(*x) }
start += elements.size
end
end
+ def [] start, num=nil
+ elements = @redis.zrange @q, start, start + (num || 0) - 1, :withscores => true
+ v = elements.each_slice(2).map do |item, at|
+ item =~ /^\d+:(\S+)$/ or raise InvalidEntryException, item
+ item = $1
+ [item, Time.at(at.to_f)]
+ end
+ num ? v : v.first
+ end
+
def size; @redis.zcard @q end
end
Please sign in to comment.
Something went wrong with that request. Please try again.