Class: ReactOnRailsPro::StreamCache::CachingComponent

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

Instance Method Summary collapse

Constructor Details

#initialize(upstream_stream, cache_key, cache_options) ⇒ CachingComponent

Returns a new instance of CachingComponent.



56
57
58
59
60
# File 'lib/react_on_rails_pro/stream_cache.rb', line 56

def initialize(upstream_stream, cache_key, cache_options)
  @upstream_stream = upstream_stream
  @cache_key = cache_key
  @cache_options = cache_options
end

Instance Method Details

#each_chunk(&block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/react_on_rails_pro/stream_cache.rb', line 62

def each_chunk(&block)
  return enum_for(:each_chunk) unless block

  buffered_chunks = []
  @upstream_stream.each_chunk do |chunk|
    buffered_chunks << chunk
    yield(chunk)
  end
  Rails.cache.write(@cache_key, buffered_chunks, @cache_options || {})
end