2012-07-18 01:14:15 -04:00
|
|
|
module Sidekiq
|
|
|
|
module Paginator
|
2014-02-01 16:51:09 -05:00
|
|
|
def page(key, pageidx=1, page_size=25, opts=nil)
|
2012-07-18 01:14:15 -04:00
|
|
|
current_page = pageidx.to_i < 1 ? 1 : pageidx.to_i
|
|
|
|
pageidx = current_page - 1
|
|
|
|
total_size = 0
|
|
|
|
items = []
|
|
|
|
starting = pageidx * page_size
|
|
|
|
ending = starting + page_size - 1
|
|
|
|
|
|
|
|
Sidekiq.redis do |conn|
|
|
|
|
type = conn.type(key)
|
|
|
|
|
|
|
|
case type
|
|
|
|
when 'zset'
|
2014-02-14 00:19:47 -05:00
|
|
|
rev = opts && opts[:reverse]
|
2014-02-01 16:51:09 -05:00
|
|
|
total_size, items = conn.multi do
|
|
|
|
conn.zcard(key)
|
|
|
|
if rev
|
|
|
|
conn.zrevrange(key, starting, ending, :with_scores => true)
|
|
|
|
else
|
|
|
|
conn.zrange(key, starting, ending, :with_scores => true)
|
|
|
|
end
|
|
|
|
end
|
2012-07-18 01:14:15 -04:00
|
|
|
when 'list'
|
2014-02-01 16:51:09 -05:00
|
|
|
total_size, items = conn.multi do
|
|
|
|
conn.llen(key)
|
|
|
|
conn.lrange(key, starting, ending)
|
|
|
|
end
|
2012-07-18 01:14:15 -04:00
|
|
|
when 'none'
|
|
|
|
return [1, 0, []]
|
|
|
|
else
|
|
|
|
raise "can't page a #{type}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[current_page, total_size, items]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|