class Kredis::Migration
Constants
- SCAN_BATCH_SIZE
Public Class Methods
new(config = :shared)
click to toggle source
# File lib/kredis/migration.rb, line 6 def initialize(config = :shared) @redis = Kredis.configured_for config # TODO: Replace script loading with `copy` command once Redis 6.2+ is the minimum supported version. @copy_sha = @redis.script "load", "redis.call('SETNX', KEYS[2], redis.call('GET', KEYS[1])); return 1;" end
Public Instance Methods
delete_all(key_pattern)
click to toggle source
# File lib/kredis/migration.rb, line 33 def delete_all(key_pattern) each_key_batch_matching(key_pattern) do |keys| @redis.del *keys end end
migrate(from:, to:)
click to toggle source
# File lib/kredis/migration.rb, line 21 def migrate(from:, to:) namespaced_to = Kredis.namespaced_key(to) if to.present? && from != namespaced_to log_migration "Migrating key #{from} to #{namespaced_to}" do @redis.evalsha @copy_sha, keys: [ from, namespaced_to ] end else log_migration "Skipping blank/unaltered migration key #{from} → #{to}" end end
migrate_all(key_pattern) { |key, *ids| ... }
click to toggle source
# File lib/kredis/migration.rb, line 12 def migrate_all(key_pattern) each_key_batch_matching(key_pattern) do |keys| keys.each do |key| ids = key.scan(/\d+/).map(&:to_i) migrate from: key, to: yield(key, *ids) end end end
Private Instance Methods
each_key_batch_matching(key_pattern) { |keys| ... }
click to toggle source
# File lib/kredis/migration.rb, line 42 def each_key_batch_matching(key_pattern, &block) cursor = "0" begin cursor, keys = @redis.scan(cursor, match: key_pattern, count: SCAN_BATCH_SIZE) @redis.pipelined { yield keys } end until cursor == "0" end
log_migration(message, &block)
click to toggle source
# File lib/kredis/migration.rb, line 50 def log_migration(message, &block) Kredis.instrument :migration, message: message, &block end