mirror of
https://github.com/discourse/discourse.git
synced 2024-12-13 09:24:15 +08:00
02245ce41f
We can fake redis transactions so that `fab!` works for redis and PG data, but it's too slow to be used indiscriminately. Instead, you can opt into it with the `use_redis_snapshotting` helper. Insofar as snapshotting allows us to `fab!` more things, it provides a speedup.
42 lines
748 B
Ruby
42 lines
748 B
Ruby
# 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 =
|
|
redis.pipelined do
|
|
keys.each do |key|
|
|
redis.dump(key)
|
|
end
|
|
end
|
|
|
|
new(keys.zip(values).delete_if { |k, v| v.nil? })
|
|
end
|
|
|
|
def initialize(dump)
|
|
@dump = dump
|
|
end
|
|
|
|
def restore(redis = Discourse.redis)
|
|
redis.pipelined do
|
|
redis.flushdb
|
|
|
|
@dump.each do |key, value|
|
|
redis.restore(key, 0, value)
|
|
end
|
|
end
|
|
|
|
nil
|
|
end
|
|
end
|