Class: ReactOnRailsPro::StreamCache

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

Defined Under Namespace

Classes: CachedChunksComponent, CachingComponent

Class Method Summary collapse

Class Method Details

.build_stream_from_chunks(chunks) ⇒ Object

Builds a stream-like object from an array of chunks.



24
25
26
27
# File 'lib/react_on_rails_pro/stream_cache.rb', line 24

def build_stream_from_chunks(chunks)
  component = CachedChunksComponent.new(chunks)
  ReactOnRailsPro::StreamDecorator.new(component)
end

.fetch_stream(cache_key) ⇒ Object

Returns a stream-like object that responds to each_chunk and yields cached chunks or nil if not present in cache.



8
9
10
11
12
13
# File 'lib/react_on_rails_pro/stream_cache.rb', line 8

def fetch_stream(cache_key)
  cached_chunks = Rails.cache.read(cache_key)
  return nil unless cached_chunks.is_a?(Array)

  build_stream_from_chunks(cached_chunks)
end

.wrap_and_cache(cache_key, upstream_stream, cache_options: nil) ⇒ Object

Wraps an upstream stream (responds to each_chunk), yields chunks downstream while buffering them, and writes the chunks array to Rails.cache on successful completion. Returns a stream-like object that responds to each_chunk.



18
19
20
21
# File 'lib/react_on_rails_pro/stream_cache.rb', line 18

def wrap_and_cache(cache_key, upstream_stream, cache_options: nil)
  component = CachingComponent.new(upstream_stream, cache_key, cache_options)
  ReactOnRailsPro::StreamDecorator.new(component)
end