Class: Thredded::CollectionToStringsWithCacheRenderer

Inherits:
ActionView::AbstractRenderer
  • Object
show all
Defined in:
lib/thredded/collection_to_strings_with_cache_renderer.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.render_threadsObject

The default number of threads to use for rendering.



9
10
11
# File 'lib/thredded/collection_to_strings_with_cache_renderer.rb', line 9

def render_threads
  @render_threads
end

Instance Method Details

#render_collection_to_strings_with_cache(view_context, collection:, partial:, expires_in:, render_threads: self.class.render_threads, locals: {}, **opts) ⇒ Object

Parameters:

  • view_context
  • collection (Array<T>)
  • partial (String)
  • expires_in (ActiveSupport::Duration)
  • render_threads (Integer) (defaults to: self.class.render_threads)

    the number of threads to use for rendering. This is useful even on MRI ruby for IO-bound operations.

  • locals (Hash) (defaults to: {})


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/thredded/collection_to_strings_with_cache_renderer.rb', line 22

def render_collection_to_strings_with_cache( # rubocop:disable Metrics/ParameterLists
  view_context, collection:, partial:, expires_in:, render_threads: self.class.render_threads, locals: {}, **opts
)
  template = @lookup_context.find_template(partial, [], true, locals.keys, {})
  collection = collection.to_a
  ActiveSupport::Notifications.instrument(:collection,
                                          identifier: template.identifier,
                                          count: collection.size) do |instrumentation_payload|
    return [] if collection.blank?

    # Result is a hash with the key represents the
    # key used for cache lookup and the value is the item
    # on which the partial is being rendered
    keyed_collection, ordered_keys = collection_by_cache_keys(collection, view_context, template)

    cache = collection_cache
    cached_partials = cache.read_multi(*keyed_collection.keys)
    instrumentation_payload[:cache_hits] = cached_partials.size if instrumentation_payload

    collection_to_render = keyed_collection.reject { |key, _| cached_partials.key?(key) }.values
    rendered_partials = render_partials(
      view_context,
      collection: collection_to_render, render_threads: render_threads,
      partial: partial, locals: locals, **opts
    ).each

    ordered_keys.map do |cache_key|
      [keyed_collection[cache_key], cached_partials[cache_key] || rendered_partials.next.tap do |rendered|
        cache.write(cache_key, rendered, expires_in: expires_in)
        cached_partials[cache_key] = rendered
      end]
    end
  end
end