@@ -8,23 +8,29 @@ module ViewComponent::Cacheable
88 extend ActiveSupport ::Concern
99
1010 included do
11- class_attribute :__vc_cache_dependencies , default : Set [ :format , :__vc_format , :identifier ]
11+
12+ class_attribute :__vc_cache_options , default : Set [ :identifier ]
13+ class_attribute :__vc_cache_dependencies , default : Set . new
1214
1315 # For caching, such as #cache_if
1416 #
1517 # @private
1618 def view_cache_dependencies
17- return if __vc_cache_dependencies . blank? || __vc_cache_dependencies . none? || __vc_cache_dependencies . nil?
19+ self . class . __vc_cache_dependencies . map { |dep | public_send ( dep ) }
20+ end
1821
19- computed_view_cache_dependencies = __vc_cache_dependencies . map { |dep | if respond_to? ( dep ) then public_send ( dep ) end }
20- combined_fragment_cache_key ( ActiveSupport ::Cache . expand_cache_key ( computed_view_cache_dependencies ) )
22+ def view_cache_options
23+ return if __vc_cache_options . blank?
24+
25+ computed_view_cache_options = __vc_cache_options . map { |opt | if respond_to? ( opt ) then public_send ( opt ) end }
26+ combined_fragment_cache_key ( ActiveSupport ::Cache . expand_cache_key ( computed_view_cache_options + component_digest ) )
2127 end
2228
2329 # Render component from cache if possible
2430 #
2531 # @private
2632 def __vc_render_cacheable ( rendered_template )
27- if __vc_cache_dependencies != [ :format , :__vc_format ]
33+ if __vc_cache_options . any?
2834 ViewComponent ::CachingRegistry . track_caching do
2935 template_fragment ( rendered_template )
3036 end
@@ -34,7 +40,7 @@ def __vc_render_cacheable(rendered_template)
3440 end
3541
3642 def template_fragment ( rendered_template )
37- if content = read_fragment ( rendered_template )
43+ if content = read_fragment
3844 @view_renderer . cache_hits [ @current_template &.virtual_path ] = :hit if defined? ( @view_renderer )
3945 content
4046 else
@@ -43,13 +49,13 @@ def template_fragment(rendered_template)
4349 end
4450 end
4551
46- def read_fragment ( rendered_template )
47- Rails . cache . fetch ( component_digest )
52+ def read_fragment
53+ Rails . cache . fetch ( view_cache_options )
4854 end
4955
5056 def write_fragment ( rendered_template )
5157 content = __vc_render_template ( rendered_template )
52- Rails . cache . fetch ( component_digest ) do
58+ Rails . cache . fetch ( view_cache_options ) do
5359 content
5460 end
5561 content
@@ -63,20 +69,19 @@ def combined_fragment_cache_key(key)
6369 end
6470
6571 def component_digest
66- component_name = self . class . name . demodulize . underscore
67- ViewComponent ::CacheDigestor . digest ( name : component_name , format : format , finder : @lookup_context , dependencies : view_cache_dependencies )
72+ ViewComponent ::CacheDigestor . new ( component : self ) . digest
6873 end
6974 end
7075
7176 class_methods do
7277 # For caching the component
7378 def cache_on ( *args )
74- __vc_cache_dependencies . merge ( args )
79+ __vc_cache_options . merge ( args )
7580 end
7681
7782 def inherited ( child )
78- child . __vc_cache_dependencies = __vc_cache_dependencies . dup
79-
83+ child . __vc_cache_options = __vc_cache_options . dup
84+
8085 super
8186 end
8287 end
0 commit comments