Sha256: 3a206ececbd597c79d2d28cf086d3650a325bd19edf3aca21c78f03cd1fca56e

Contents?: true

Size: 2 KB

Versions: 32

Compression:

Stored size: 2 KB

Contents

# frozen_string_literal: true

module SidekiqUniqueJobs
  module Redis
    #
    # Class SortedSet provides convenient access to redis sorted sets
    #
    # @author Mikael Henriksson <mikael@mhenrixon.com>
    #
    class SortedSet < Entity
      #
      # Return entries for this sorted set
      #
      # @param [true,false] with_scores true return
      #
      # @return [Array<Object>] when given with_scores: false
      # @return [Hash] when given with_scores: true
      #
      def entries(with_scores: true)
        entrys = redis { |conn| conn.zrange(key, 0, -1, with_scores: with_scores) }
        return entrys unless with_scores

        entrys.each_with_object({}) { |pair, hash| hash[pair[0]] = pair[1] }
      end

      #
      # Adds a value to the sorted set
      #
      # @param [Array<Float, String>, String] values the values to add
      #
      # @return [Boolean, Integer] <description>
      #
      def add(values)
        redis do |conn|
          if values.is_a?(Array)
            conn.zadd(key, values)
          else
            conn.zadd(key, now_f, values)
          end
        end
      end

      #
      # Return the zrak of the member
      #
      # @param [String] member the member to pull rank on
      #
      # @return [Integer]
      #
      def rank(member)
        redis { |conn| conn.zrank(key, member) }
      end

      #
      # Return score for a member
      #
      # @param [String] member the member for which score
      #
      # @return [Integer, Float]
      #
      def score(member)
        redis { |conn| conn.zscore(key, member) }
      end

      #
      # Clears the sorted set from all entries
      #
      #
      # @return [Integer] number of entries removed
      #
      def clear
        redis { |conn| conn.zremrangebyrank(key, 0, count) }
      end

      #
      # Returns the count for this sorted set
      #
      #
      # @return [Integer] the number of entries
      #
      def count
        redis { |conn| conn.zcard(key) }
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-7.1.33 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.32 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.31 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.30 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.29 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.28 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.27 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.26 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.25 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.24 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.23 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.22 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.21 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.20 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.19 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.18 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.17 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.16 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.15 lib/sidekiq_unique_jobs/redis/sorted_set.rb
sidekiq-unique-jobs-7.1.14 lib/sidekiq_unique_jobs/redis/sorted_set.rb