Module: SidekiqUniqueJobs::Digests

Extended by:
Digests
Includes:
Connection, Logging
Included in:
Digests
Defined in:
lib/sidekiq_unique_jobs/digests.rb

Overview

Utility module to help manage unique digests in redis.

Constant Summary collapse

DEFAULT_COUNT =
1_000
SCAN_PATTERN =
'*'
CHUNK_SIZE =
100

Instance Method Summary collapse

Methods included from Connection

#redis

Methods included from Logging

#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger, #logging_context

Instance Method Details

#all(pattern: SCAN_PATTERN, count: DEFAULT_COUNT) ⇒ Array<String>

Return unique digests matching pattern

Parameters:

  • pattern (String) (defaults to: SCAN_PATTERN)

    a pattern to match with

  • count (Integer) (defaults to: DEFAULT_COUNT)

    the maximum number to match

Returns:

  • (Array<String>)

    with unique digests



21
22
23
# File 'lib/sidekiq_unique_jobs/digests.rb', line 21

def all(pattern: SCAN_PATTERN, count: DEFAULT_COUNT)
  redis { |conn| conn.sscan_each(UNIQUE_SET, match: pattern, count: count).to_a }
end

#countInteger

Get a total count of unique digests

Returns:

  • (Integer)

    number of digests



45
46
47
# File 'lib/sidekiq_unique_jobs/digests.rb', line 45

def count
  redis { |conn| conn.scard(UNIQUE_SET) }
end

#del(digest: nil, pattern: nil, count: DEFAULT_COUNT) ⇒ Array<String>

Deletes unique digest either by a digest or pattern

Parameters:

  • digest (String) (defaults to: nil)

    the full digest to delete

  • pattern (String) (defaults to: nil)

    a key pattern to match with

  • count (Integer) (defaults to: DEFAULT_COUNT)

    the maximum number

Returns:

  • (Array<String>)

    with unique digests

Raises:

  • (ArgumentError)

    when both pattern and digest are nil



56
57
58
59
60
61
# File 'lib/sidekiq_unique_jobs/digests.rb', line 56

def del(digest: nil, pattern: nil, count: DEFAULT_COUNT)
  return delete_by_pattern(pattern, count: count) if pattern
  return delete_by_digest(digest) if digest

  raise ArgumentError, 'either digest or pattern need to be provided'
end

#page(pattern: SCAN_PATTERN, cursor: 0, page_size: 100) ⇒ Array<String>

Paginate unique digests

Parameters:

  • pattern (String) (defaults to: SCAN_PATTERN)

    a pattern to match with

  • page (Integer)

    the current cursor position

  • count (Integer)

    the maximum number to match

Returns:

  • (Array<String>)

    with unique digests



31
32
33
34
35
36
37
38
39
40
# File 'lib/sidekiq_unique_jobs/digests.rb', line 31

def page(pattern: SCAN_PATTERN, cursor: 0, page_size: 100)
  redis do |conn|
    total_size, digests = conn.multi do
      conn.scard(UNIQUE_SET)
      conn.sscan(UNIQUE_SET, cursor, match: pattern, count: page_size)
    end

    [total_size, digests[0], digests[1]]
  end
end