BigW Consortium Gitlab

reactive_caching_helpers.rb 1.42 KB
Newer Older
1 2 3 4 5
module ReactiveCachingHelpers
  def reactive_cache_key(subject, *qualifiers)
    ([subject.class.reactive_cache_key.call(subject)].flatten + qualifiers).join(':')
  end

6 7 8 9 10
  def alive_reactive_cache_key(subject, *qualifiers)
    reactive_cache_key(subject, *(qualifiers + ['alive']))
  end

  def stub_reactive_cache(subject = nil, data = nil, *qualifiers)
11 12
    allow(ReactiveCachingWorker).to receive(:perform_async)
    allow(ReactiveCachingWorker).to receive(:perform_in)
13
    write_reactive_cache(subject, data, *qualifiers) if data
14 15
  end

16 17
  def read_reactive_cache(subject, *qualifiers)
    Rails.cache.read(reactive_cache_key(subject, *qualifiers))
18 19
  end

20 21 22
  def write_reactive_cache(subject, data, *qualifiers)
    start_reactive_cache_lifetime(subject, *qualifiers)
    Rails.cache.write(reactive_cache_key(subject, *qualifiers), data)
23 24
  end

25 26
  def reactive_cache_alive?(subject, *qualifiers)
    Rails.cache.read(alive_reactive_cache_key(subject, *qualifiers))
27 28
  end

29 30
  def invalidate_reactive_cache(subject, *qualifiers)
    Rails.cache.delete(alive_reactive_cache_key(subject, *qualifiers))
31 32
  end

33 34
  def start_reactive_cache_lifetime(subject, *qualifiers)
    Rails.cache.write(alive_reactive_cache_key(subject, *qualifiers), true)
35 36 37
  end

  def expect_reactive_cache_update_queued(subject)
38 39 40
    expect(ReactiveCachingWorker)
      .to receive(:perform_in)
      .with(subject.class.reactive_cache_refresh_interval, subject.class, subject.id)
41 42
  end
end