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.



43
44
45
46
47
# File 'lib/react_on_rails_pro/stream_cache.rb', line 43

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



49
50
51
52
53
54
55
56
57
58
# File 'lib/react_on_rails_pro/stream_cache.rb', line 49

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