Class: SidekiqUniqueJobs::Redis::SortedSet

Inherits:
Entity
  • Object
show all
Defined in:
lib/sidekiq_unique_jobs/redis/sorted_set.rb

Overview

Class SortedSet provides convenient access to redis sorted sets

Author:

Direct Known Subclasses

Changelog, Digests

Constant Summary collapse

DEFAULT_COUNT =

Returns the number of matches to return by default.

Returns:

  • (Integer)

    the number of matches to return by default

1_000
SCAN_PATTERN =

Returns the default pattern to use for matching.

Returns:

  • (String)

    the default pattern to use for matching

"*"

Instance Attribute Summary

Attributes inherited from Entity

#key

Instance Method Summary collapse

Methods inherited from Entity

#exist?, #expires?, #initialize, #pttl, #ttl

Methods included from Timing

clock_stamp, now_f, time_source, timed

Methods included from JSON

dump_json, load_json, safe_load_json

Methods included from Script::Caller

call_script, debug_lua, do_call, extract_args, max_history, normalize_argv, now_f, redis_version

Methods included from Logging

#build_message, included, #log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger, #logging_context, #with_configured_loggers_context, #with_logging_context

Constructor Details

This class inherits a constructor from SidekiqUniqueJobs::Redis::Entity

Instance Method Details

#add(values) ⇒ Boolean, Integer

Adds a value to the sorted set

Parameters:

Returns:

  • (Boolean, Integer)

    <description>



41
42
43
44
45
46
47
48
49
# File 'lib/sidekiq_unique_jobs/redis/sorted_set.rb', line 41

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

#clearInteger

Clears the sorted set from all entries

Returns:

  • (Integer)

    number of entries removed



79
80
81
# File 'lib/sidekiq_unique_jobs/redis/sorted_set.rb', line 79

def clear
  redis { |conn| conn.zremrangebyrank(key, 0, count) }
end

#countInteger

Returns the count for this sorted set

Returns:

  • (Integer)

    the number of entries



89
90
91
# File 'lib/sidekiq_unique_jobs/redis/sorted_set.rb', line 89

def count
  redis { |conn| conn.zcard(key) }
end

#entries(with_scores: true) ⇒ Array<Object>, Hash

Return entries for this sorted set

Parameters:

  • with_scores (true, false) (defaults to: true)

    true return

Returns:

  • (Array<Object>)

    when given with_scores: false

  • (Hash)

    when given with_scores: true



26
27
28
29
30
31
32
# File 'lib/sidekiq_unique_jobs/redis/sorted_set.rb', line 26

def entries(with_scores: true)
  return redis { |conn| conn.zrange(key, 0, -1) } unless with_scores

  redis { |conn| conn.zrange(key, 0, -1, "withscores") }.each_with_object({}) do |pair, hash|
    hash[pair[0]] = pair[1]
  end
end

#rank(member) ⇒ Integer

Return the zrak of the member

Parameters:

  • member (String)

    the member to pull rank on

Returns:

  • (Integer)


58
59
60
# File 'lib/sidekiq_unique_jobs/redis/sorted_set.rb', line 58

def rank(member)
  redis { |conn| conn.zrank(key, member) }
end

#score(member) ⇒ Integer, Float

Return score for a member

Parameters:

  • member (String)

    the member for which score

Returns:

  • (Integer, Float)


69
70
71
# File 'lib/sidekiq_unique_jobs/redis/sorted_set.rb', line 69

def score(member)
  redis { |conn| conn.zscore(key, member) }
end