Module: SidekiqUniqueJobs::Util
- Extended by:
- Util
- Includes:
- Connection, Logging
- Included in:
- Util
- Defined in:
- lib/sidekiq_unique_jobs/util.rb
Overview
Utility module to help manage unique keys in redis. Useful for deleting keys that for whatever reason wasn’t deleted
Constant Summary collapse
- DEFAULT_COUNT =
1_000
- SCAN_PATTERN =
"*"
Instance Method Summary collapse
-
#del(pattern = SCAN_PATTERN, count = 0) ⇒ Integer
Deletes unique keys from redis.
-
#keys(pattern = SCAN_PATTERN, count = DEFAULT_COUNT) ⇒ Array<String>
Find unique keys in redis.
-
#keys_with_ttl(pattern = SCAN_PATTERN, count = DEFAULT_COUNT) ⇒ Hash<String, Integer>
Find unique keys with ttl.
Methods included from Connection
Methods included from Logging
#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger, #logging_context
Instance Method Details
#del(pattern = SCAN_PATTERN, count = 0) ⇒ Integer
Deletes unique keys from redis
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 46 def del(pattern = SCAN_PATTERN, count = 0) raise ArgumentError, "Please provide a number of keys to delete greater than zero" if count.zero? pattern = suffix(pattern) log_debug { "Deleting keys by: #{pattern}" } keys, time = timed { keys(pattern, count) } key_size = keys.size log_debug { "#{key_size} keys found in #{time} sec." } _, time = timed { batch_delete(keys) } log_debug { "Deleted #{key_size} keys in #{time} sec." } key_size end |
#keys(pattern = SCAN_PATTERN, count = DEFAULT_COUNT) ⇒ Array<String>
Find unique keys in redis
21 22 23 24 25 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 21 def keys(pattern = SCAN_PATTERN, count = DEFAULT_COUNT) return redis(&:keys) if pattern.nil? redis { |conn| conn.scan_each(match: prefix(pattern), count: count).to_a } end |
#keys_with_ttl(pattern = SCAN_PATTERN, count = DEFAULT_COUNT) ⇒ Hash<String, Integer>
Find unique keys with ttl
31 32 33 34 35 36 37 38 39 |
# File 'lib/sidekiq_unique_jobs/util.rb', line 31 def keys_with_ttl(pattern = SCAN_PATTERN, count = DEFAULT_COUNT) hash = {} redis do |conn| conn.scan_each(match: prefix(pattern), count: count).each do |key| hash[key] = conn.ttl(key) end end hash end |