Class: ReactOnRailsPro::Cache
- Inherits:
-
Object
- Object
- ReactOnRailsPro::Cache
show all
- Defined in:
- lib/react_on_rails_pro/cache.rb,
lib/react_on_rails_pro/cache/tag_index.rb,
lib/react_on_rails_pro/cache/revalidates.rb,
sig/react_on_rails_pro/cache.rbs
Defined Under Namespace
Modules: Revalidates
Classes: TagIndex
Constant Summary
collapse
- ACTIVE_SUPPORT_EXPIRES_AT_VERSION =
Gem::Version.new("7.0.0")
- EXPIRED_CACHE_WRITE_TTL =
seconds; minimum positive TTL for race-expired writes
1
- RSC_BUNDLE_MISSING_CACHE_KEY =
"rsc-bundle-missing"
Class Method Summary
collapse
Class Method Details
.base_cache_key(type, prerender: nil) ⇒ Array[String]
Cache keys by React on Rails Pro should build upon this base
Provide prerender: true in order to include bundle hashes in the list of keys.
Bundle hashes are necessary so that any changes to rendered output fault the cache.
.cache_supports_expires_at? ⇒ Boolean
.cache_write_expired?(cache_options) ⇒ Boolean
65
66
67
68
69
70
71
72
|
# File 'lib/react_on_rails_pro/cache.rb', line 65
def cache_write_expired?(cache_options)
return false unless cache_options&.key?(:expires_at)
expires_at = cache_options[:expires_at]
return false if expires_at && unsupported_expires_at_with_explicit_expires_in?(cache_options)
expires_at && expires_at.to_time.to_f <= Time.now.to_f
end
|
.cache_write_options(cache_options) ⇒ Hash[Symbol, untyped]?
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/react_on_rails_pro/cache.rb', line 47
def cache_write_options(cache_options)
return cache_options unless cache_options&.key?(:expires_at)
expires_at = cache_options[:expires_at]
return cache_options unless expires_at
return cache_options.except(:expires_at) if unsupported_expires_at_with_explicit_expires_in?(cache_options)
expires_in = expires_at.to_time.to_f - Time.now.to_f
return cache_options.merge(expires_in: EXPIRED_CACHE_WRITE_TTL).except(:expires_at) if expires_in <= 0
return supported_expires_at_write_options(cache_options) if cache_supports_expires_at?
return cache_options.except(:expires_at) unless cache_options[:expires_in].nil?
cache_options.merge(expires_in:).except(:expires_at)
end
|
.dependencies_cache_key ⇒ String?
41
42
43
44
45
|
# File 'lib/react_on_rails_pro/cache.rb', line 41
def normalize_tags(tags)
return [] if tags.nil? || (tags.is_a?(Array) && tags.empty?)
TagIndex.normalize_tags(tags)
end
|
.react_component_cache_key(component_name, options) ⇒ Array[untyped]
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/react_on_rails_pro/cache.rb', line 171
def react_component_cache_key(component_name, options)
cache_key_option = options[:cache_key]
cache_key_value = if cache_key_option.respond_to?(:call)
cache_key_option.call
else
cache_key_option
end
[
*base_cache_key("ror_component", prerender: options[:prerender]),
dependencies_cache_key,
component_name,
cache_key_value
].compact
end
|
This method returns an undefined value.
35
36
37
38
39
|
# File 'lib/react_on_rails_pro/cache.rb', line 35
def register_normalized_tags(normalized_tags, cache_key, cache_options)
return if normalized_tags.blank?
TagIndex.register_normalized(normalized_tags, cache_key, cache_options || {})
end
|
This method returns an undefined value.
Registers cache tags for an already-written cache entry so a later
ReactOnRailsPro.revalidate_tag can delete it. Call after a successful
cache write (never on a cache hit). No-op when tags are nil or [].
See ReactOnRailsPro::Cache::TagIndex for the v1 index semantics
(best-effort, lossy-OK; correctness bounded by :expires_in).
31
32
33
|
# File 'lib/react_on_rails_pro/cache.rb', line 31
def register_tags(tags, cache_key, cache_options)
register_normalized_tags(normalize_tags(tags), cache_key, cache_options)
end
|
Deletes every cached component entry registered under the given tags
and clears the tag index entries. Tags accept the same forms as the
cache_tags: helper option. Blank tags (nil/empty/whitespace) are
silently ignored at the revalidation boundary (unlike registration,
which raises on blank tags). Missing/evicted index entries are a no-op.
Returns the number of cache entries deleted.
94
95
96
97
98
99
|
# File 'lib/react_on_rails_pro/cache.rb', line 94
def revalidate_tags(*tags)
meaningful_tags = meaningful_revalidation_tags(tags)
return 0 if meaningful_tags.empty?
TagIndex.revalidate(*meaningful_tags)
end
|
.supported_expires_at_write_options(cache_options) ⇒ Object
78
79
80
81
82
|
# File 'lib/react_on_rails_pro/cache.rb', line 78
def supported_expires_at_write_options(cache_options)
return cache_options.except(:expires_in) if cache_options.key?(:expires_in)
cache_options
end
|
.unsupported_expires_at_with_explicit_expires_in?(cache_options) ⇒ Boolean
84
85
86
|
# File 'lib/react_on_rails_pro/cache.rb', line 84
def unsupported_expires_at_with_explicit_expires_in?(cache_options)
!cache_supports_expires_at? && cache_options.key?(:expires_in) && !cache_options[:expires_in].nil?
end
|
.use_cache?(options) ⇒ Boolean
134
135
136
137
138
139
140
141
142
|
# File 'lib/react_on_rails_pro/cache.rb', line 134
def use_cache?(options)
if options.key?(:if)
options[:if]
elsif options.key?(:unless)
!options[:unless]
else
true
end
end
|