@@ -8,23 +8,29 @@ module ViewComponent::Cacheable
8
8
extend ActiveSupport ::Concern
9
9
10
10
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
12
14
13
15
# For caching, such as #cache_if
14
16
#
15
17
# @private
16
18
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
18
21
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 ) )
21
27
end
22
28
23
29
# Render component from cache if possible
24
30
#
25
31
# @private
26
32
def __vc_render_cacheable ( rendered_template )
27
- if __vc_cache_dependencies != [ :format , :__vc_format ]
33
+ if __vc_cache_options . any?
28
34
ViewComponent ::CachingRegistry . track_caching do
29
35
template_fragment ( rendered_template )
30
36
end
@@ -34,7 +40,7 @@ def __vc_render_cacheable(rendered_template)
34
40
end
35
41
36
42
def template_fragment ( rendered_template )
37
- if content = read_fragment ( rendered_template )
43
+ if content = read_fragment
38
44
@view_renderer . cache_hits [ @current_template &.virtual_path ] = :hit if defined? ( @view_renderer )
39
45
content
40
46
else
@@ -43,13 +49,13 @@ def template_fragment(rendered_template)
43
49
end
44
50
end
45
51
46
- def read_fragment ( rendered_template )
47
- Rails . cache . fetch ( component_digest )
52
+ def read_fragment
53
+ Rails . cache . fetch ( view_cache_options )
48
54
end
49
55
50
56
def write_fragment ( rendered_template )
51
57
content = __vc_render_template ( rendered_template )
52
- Rails . cache . fetch ( component_digest ) do
58
+ Rails . cache . fetch ( view_cache_options ) do
53
59
content
54
60
end
55
61
content
@@ -63,20 +69,19 @@ def combined_fragment_cache_key(key)
63
69
end
64
70
65
71
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
68
73
end
69
74
end
70
75
71
76
class_methods do
72
77
# For caching the component
73
78
def cache_on ( *args )
74
- __vc_cache_dependencies . merge ( args )
79
+ __vc_cache_options . merge ( args )
75
80
end
76
81
77
82
def inherited ( child )
78
- child . __vc_cache_dependencies = __vc_cache_dependencies . dup
79
-
83
+ child . __vc_cache_options = __vc_cache_options . dup
84
+
80
85
super
81
86
end
82
87
end
0 commit comments