2021-12-10 14:25:26 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RedisSnapshot
|
|
|
|
def self.begin_faux_transaction(redis = Discourse.redis)
|
|
|
|
@stack ||= []
|
|
|
|
@stack.push(RedisSnapshot.load(redis))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.end_faux_transaction(redis = Discourse.redis)
|
|
|
|
@stack.pop.restore(redis)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.load(redis = Discourse.redis)
|
|
|
|
keys = redis.keys
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
values = redis.pipelined { |batch| keys.each { |key| batch.dump(key) } }
|
2021-12-10 14:25:26 -06:00
|
|
|
|
|
|
|
new(keys.zip(values).delete_if { |k, v| v.nil? })
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(dump)
|
|
|
|
@dump = dump
|
|
|
|
end
|
|
|
|
|
|
|
|
def restore(redis = Discourse.redis)
|
2022-05-09 17:19:02 -05:00
|
|
|
redis.pipelined do |batch|
|
|
|
|
batch.flushdb
|
2021-12-10 14:25:26 -06:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
@dump.each { |key, value| batch.restore(key, 0, value) }
|
2021-12-10 14:25:26 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|