Class: RedisSnapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_snapshot.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dump) ⇒ RedisSnapshot

Returns a new instance of RedisSnapshot.



21
22
23
# File 'lib/redis_snapshot.rb', line 21

def initialize(dump)
  @dump = dump
end

Class Method Details

.begin_faux_transaction(redis = Discourse.redis) ⇒ Object



4
5
6
7
# File 'lib/redis_snapshot.rb', line 4

def self.begin_faux_transaction(redis = Discourse.redis)
  @stack ||= []
  @stack.push(RedisSnapshot.load(redis))
end

.end_faux_transaction(redis = Discourse.redis) ⇒ Object



9
10
11
# File 'lib/redis_snapshot.rb', line 9

def self.end_faux_transaction(redis = Discourse.redis)
  @stack.pop.restore(redis)
end

.load(redis = Discourse.redis) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/redis_snapshot.rb', line 13

def self.load(redis = Discourse.redis)
  keys = redis.keys

  values = redis.pipelined { |batch| keys.each { |key| batch.dump(key) } }

  new(keys.zip(values).delete_if { |k, v| v.nil? })
end

Instance Method Details

#restore(redis = Discourse.redis) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/redis_snapshot.rb', line 25

def restore(redis = Discourse.redis)
  redis.pipelined do |batch|
    batch.flushdb

    @dump.each { |key, value| batch.restore(key, 0, value) }
  end

  nil
end