Last active
October 17, 2025 15:36
-
-
Save andrepiske/124f7c127b4af0ee19de9e9ca54ca958 to your computer and use it in GitHub Desktop.
redis_and_queues.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| r = Redis.new(url: ENV['REDIS_URL']) | |
| def fetch_all_keys(r, batch: 10_000, match: nil) | |
| cursor = '0' | |
| all_keys = [] | |
| loop do | |
| cursor, k = r.scan(cursor, count: batch, match: match) | |
| all_keys += k | |
| break if cursor == '0' | |
| end | |
| all_keys | |
| end | |
| def delete_these_keys(r, all_keys, batch_size: 500) | |
| all_keys.each_slice(batch_size) { |x| r.del(x) } | |
| end | |
| def hist_groups(gfn, dataset) | |
| hist = {} | |
| dataset.each { |e| g = gfn.call(e); hist[g] = hist[g].to_i + 1 } | |
| hist.sort_by { |k, v| -v } | |
| end | |
| def hist_groups_vals(gfn, dataset) | |
| hist = {} | |
| dataset.each { |e| g = gfn.call(e); hist[g] = Array(hist[g]) + [e] } | |
| hist.sort_by { |k, v| -v.length } | |
| end | |
| def bytekb(v) | |
| v < 1024 ? v : (v < 1048576 ? ("%.2fkb" % [v/1024.0]) : (v < 1073741824 ? ("%.2fmb" % [v/1048576.0]) : ("%.2fgb" % [v/ 1073741824]))) | |
| end | |
| def print_keys_by_size(r, only: nil, batch: 10_000) | |
| all_keys = fetch_all_keys(r, batch: batch) | |
| us = all_keys.map { |k| [k, r.call('memory', 'usage', k)] }.select { |_, v| v }.sort_by { |_, v| -v }.map { |a, v| [a, bytekb(v)] } | |
| us = us[0...only] if only | |
| us.each { |k, v| puts("#{v} -> #{k}") } | |
| nil | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment