1# frozen_string_literal: true
2
3require "redis"
4
5class RedisAddresses
6 def initialize(redis, currency)
7 @redis = redis
8 @currency = currency.downcase
9 end
10
11 def each_user
12 # I picked 1000 because it made a relatively trivial case take
13 # 15 seconds instead of forever.
14 # Basically it's "how long does each command take"
15 # The lower it is (default is 10), it will go back and forth
16 # to the client a ton
17 @redis.scan_each(
18 match: "jmp_customer_#{@currency}_addresses-*",
19 count: 1000
20 ) do |key|
21 yield key, @redis.smembers(key)
22 end
23 end
24
25 # This returns a hash
26 # The keys are the bitcoin addresses, the values are all of the keys which
27 # contain that address
28 # If there are no duplicates, then each value will be a singleton list
29 def get_addresses_with_users
30 addrs = Hash.new { |h, k| h[k] = [] }
31
32 # 1000 because it made a relatively trivial case take 15 seconds
33 # instead of forever.
34 # Basically it's "how long does each command take"
35 # The lower it is (default is 10), it will go back and forth
36 # to the client a ton
37 pattern = "jmp_customer_#{@currency}_addresses-*"
38 @redis.scan_each(match: pattern, count: 1000) do |key|
39 @redis.smembers(key).each do |addr|
40 addrs[addr] << key
41 end
42 end
43
44 addrs
45 end
46end