2021-12-10 15:25:26 -05: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
|
|
|
|
|
|
|
|
values =
|
2022-05-09 18:19:02 -04:00
|
|
|
redis.pipelined do |batch|
|
2021-12-10 15:25:26 -05:00
|
|
|
keys.each do |key|
|
2022-05-09 18:19:02 -04:00
|
|
|
batch.dump(key)
|
2021-12-10 15:25:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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 18:19:02 -04:00
|
|
|
redis.pipelined do |batch|
|
|
|
|
batch.flushdb
|
2021-12-10 15:25:26 -05:00
|
|
|
|
|
|
|
@dump.each do |key, value|
|
2022-05-09 18:19:02 -04:00
|
|
|
batch.restore(key, 0, value)
|
2021-12-10 15:25:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|