Sha256: c5296aa1dc6a8b156bb44ae286e4b90fc704d9e7a80bca2f3b5c557a26b5cc72

Contents?: true

Size: 1.02 KB

Versions: 23

Compression:

Stored size: 1.02 KB

Contents

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)

        case type
        when 'zset'
          rev = opts && opts[:reverse]
          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
        when 'list'
          total_size, items = conn.multi do
            conn.llen(key)
            conn.lrange(key, starting, ending)
          end
        when 'none'
          return [1, 0, []]
        else
          raise "can't page a #{type}"
        end
      end

      [current_page, total_size, items]
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
sidekiq-3.3.3 lib/sidekiq/paginator.rb
sidekiq-3.3.2 lib/sidekiq/paginator.rb
sidekiq-3.3.1 lib/sidekiq/paginator.rb
sidekiq-3.3.0 lib/sidekiq/paginator.rb
sidekiq-3.2.6 lib/sidekiq/paginator.rb
sidekiq-2.17.8 lib/sidekiq/paginator.rb
sidekiq-3.2.5 lib/sidekiq/paginator.rb
sidekiq-3.2.4 lib/sidekiq/paginator.rb
sidekiq-3.2.3 lib/sidekiq/paginator.rb
sidekiq-3.2.2 lib/sidekiq/paginator.rb
sidekiq-3.2.1 lib/sidekiq/paginator.rb
sidekiq-3.2.0 lib/sidekiq/paginator.rb
sidekiq-3.1.4 lib/sidekiq/paginator.rb
sidekiq-3.1.3 lib/sidekiq/paginator.rb
sidekiq-3.1.2 lib/sidekiq/paginator.rb
sidekiq-3.1.1 lib/sidekiq/paginator.rb
sidekiq-3.1.0 lib/sidekiq/paginator.rb
sidekiq-3.0.2 lib/sidekiq/paginator.rb
sidekiq-3.0.1 lib/sidekiq/paginator.rb
sidekiq-3.0.0 lib/sidekiq/paginator.rb