1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/lib/sidekiq/paginator.rb
Jean byroot Boussier aaac999c6d
Add a compatibility layer for redis-client (#5298)
* Add a compatibility layer for `redis-client`

As discussed in https://github.com/mperham/sidekiq/pull/5253

Switching entirely to redis-client is deemed risky, so instead
we can support both.

All is needed is a small translation layer, and some very minimal
adjustments in the callers.

Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
Co-authored-by: Mike Perham <mperham@gmail.com>
2022-05-10 12:25:04 -07:00

47 lines
1.3 KiB
Ruby

# frozen_string_literal: true
module Sidekiq
module Paginator
def page(key, pageidx = 1, page_size = 25, opts = nil)
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)
rev = opts && opts[:reverse]
case type
when "zset"
total_size, items = conn.multi { |transaction|
transaction.zcard(key)
if rev
transaction.zrevrange(key, starting, ending, withscores: true)
else
transaction.zrange(key, starting, ending, withscores: true)
end
}
[current_page, total_size, items]
when "list"
total_size, items = conn.multi { |transaction|
transaction.llen(key)
if rev
transaction.lrange(key, -ending - 1, -starting - 1)
else
transaction.lrange(key, starting, ending)
end
}
items.reverse! if rev
[current_page, total_size, items]
when "none"
[1, 0, []]
else
raise "can't page a #{type}"
end
end
end
end
end