Module: ReactOnRailsPro::StreamCacheWrites

Defined in:
lib/react_on_rails_pro/concerns/stream.rb

Class Method Summary collapse

Class Method Details

.build(cache_key:, chunks:, normalized_cache_tags:, raw_cache_options:) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/react_on_rails_pro/concerns/stream.rb', line 23

def build(cache_key:, chunks:, normalized_cache_tags:, raw_cache_options:)
  return if ReactOnRailsPro::Cache.cache_write_expired?(raw_cache_options)

  {
    cache_key:,
    chunks:,
    normalized_cache_tags:,
    raw_cache_options: raw_cache_options&.dup || {}
  }
end

.flush(pending_writes) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/react_on_rails_pro/concerns/stream.rb', line 34

def flush(pending_writes)
  Array(pending_writes).each do |cache_write|
    write(cache_write)
  rescue StandardError => e
    log_failure(e)
  end
end

.log_failure(exception) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/react_on_rails_pro/concerns/stream.rb', line 55

def log_failure(exception)
  Rails.logger.warn(
    "[React on Rails Pro] Failed to write streamed cache entry after response drain: " \
    "#{exception.class}: #{exception.message}"
  )
rescue StandardError
  # Cache write failure logging must not keep a fully drained response open.
end

.write(cache_write) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/react_on_rails_pro/concerns/stream.rb', line 42

def write(cache_write)
  raw_cache_options = cache_write[:raw_cache_options]
  return if ReactOnRailsPro::Cache.cache_write_expired?(raw_cache_options)

  cache_options = ReactOnRailsPro::Cache.cache_write_options(raw_cache_options)
  Rails.cache.write(cache_write[:cache_key], cache_write[:chunks], cache_options)
  ReactOnRailsPro::Cache.register_normalized_tags(
    cache_write[:normalized_cache_tags],
    cache_write[:cache_key],
    cache_options
  )
end