Class: ReactOnRailsPro::Cache
- Inherits:
-
Object
- Object
- ReactOnRailsPro::Cache
- Defined in:
- lib/react_on_rails_pro/cache.rb
Class Method Summary collapse
-
.base_cache_key(type, prerender: nil) ⇒ Object
Cache keys by React on Rails Pro should build upon this base Provide prerender: true in order to include bundle hash in the list of keys.
- .dependencies_cache_key ⇒ Object
-
.fetch_react_component(component_name, options) ⇒ Object
options can include :compress, :expires_in, :race_condition_ttl and other options.
- .react_component_cache_key(component_name, options) ⇒ Object
- .use_cache?(options) ⇒ Boolean
Class Method Details
.base_cache_key(type, prerender: nil) ⇒ Object
Cache keys by React on Rails Pro should build upon this base Provide prerender: true in order to include bundle hash in the list of keys. The bundle hash is necessary so that any changes to the bundle fault the cache.
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/react_on_rails_pro/cache.rb', line 57 def base_cache_key(type, prerender: nil) keys = [ type, ReactOnRails::VERSION, ReactOnRailsPro::VERSION ] # TODO: Move comment over to test # We only care about the bundle hash if prerendering because we're not caching anything # that would be generated by the bundle. keys.push(ReactOnRailsPro::Utils.bundle_hash) if prerender keys end |
.dependencies_cache_key ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/react_on_rails_pro/cache.rb', line 71 def dependencies_cache_key # https://github.com/shakacode/react_on_rails_pro/issues/32 # https://github.com/shakacode/react_on_rails/issues/39#issuecomment-143472325 return @dependency_checksum if @dependency_checksum.present? && !Rails.env.development? return nil unless ReactOnRailsPro.configuration.dependency_globs.present? @dependency_checksum = ReactOnRailsPro::Utils.digest_of_globs( ReactOnRailsPro.configuration.dependency_globs ).hexdigest end |
.fetch_react_component(component_name, options) ⇒ Object
options can include :compress, :expires_in, :race_condition_ttl and other options
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/react_on_rails_pro/cache.rb', line 23 def fetch_react_component(component_name, ) if use_cache?() cache_key = react_component_cache_key(component_name, ) Rails.logger.debug { "React on Rails Pro cache_key is #{cache_key.inspect}" } = [:cache_options] cache_hit = true result = Rails.cache.fetch(cache_key, ) do cache_hit = false yield end # Pass back the cache key in the results only if the result is a Hash if result.is_a?(Hash) result[:RORP_CACHE_KEY] = cache_key result[:RORP_CACHE_HIT] = cache_hit end result else yield end end |
.react_component_cache_key(component_name, options) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/react_on_rails_pro/cache.rb', line 83 def react_component_cache_key(component_name, ) cache_key_option = [:cache_key] cache_key_value = if cache_key_option.respond_to?(:call) cache_key_option.call else cache_key_option end # NOTE: Rails seems to do this automatically: ActiveSupport::Cache.expand_cache_key(keys) [ *base_cache_key("ror_component", prerender: [:prerender]), dependencies_cache_key, component_name, cache_key_value ].compact end |
.use_cache?(options) ⇒ Boolean
44 45 46 47 48 49 50 51 52 |
# File 'lib/react_on_rails_pro/cache.rb', line 44 def use_cache?() if .key?(:if) [:if] elsif .key?(:unless) ![:unless] else true end end |