Class: RailsPulse::CachesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/rails_pulse/caches_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#set_pagination_limit

Instance Method Details

#showObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
# File 'app/controllers/rails_pulse/caches_controller.rb', line 3

def show
  @component_id = params[:id]
  @context = params[:context]
  @cache_key = ComponentCacheKey.build(@component_id, @context)

  # Preserve component options before refresh
  existing_options = {}
  if params[:refresh]
    existing_cache = Rails.cache.read(@cache_key)
    existing_options = existing_cache[:component_options] if existing_cache&.dig(:component_options)
    Rails.cache.delete(@cache_key)
  end

  # Check if cache exists with just options (from render_skeleton_with_frame)
  cached_data = Rails.cache.read(@cache_key)
  if cached_data && !cached_data[:component_data]
    # Merge options with full data
    cached_data = {
      component_data: calculate_component_data,
      cached_at: Time.current,
      component_options: cached_data[:component_options] || {}
    }
    Rails.cache.write(@cache_key, cached_data, expires_in: ComponentCacheKey.cache_expires_in)
  elsif !cached_data
    # No cache exists, create new one (use preserved options if refreshing)
    cached_data = {
      component_data: calculate_component_data,
      cached_at: Time.current,
      component_options: existing_options
    }
    Rails.cache.write(@cache_key, cached_data, expires_in: ComponentCacheKey.cache_expires_in)
  end

  @component_data = cached_data[:component_data]
  @cached_at = cached_data[:cached_at]
  @component_options = cached_data[:component_options] || {}

  # Update cached_at timestamp in component options if refresh action exists
  if params[:refresh] && @component_options[:actions]
    update_cached_at_in_actions(@component_options[:actions], @cached_at)
    # Update the unified cache with new cached_at timestamp
    cached_data[:component_options] = @component_options
    Rails.cache.write(@cache_key, cached_data, expires_in: ComponentCacheKey.cache_expires_in)
  end
end