Class: SvelteOnRails::Lib::CacheWarmer

Inherits:
Object
  • Object
show all
Defined in:
lib/svelte_on_rails/lib/cache_warmer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCacheWarmer

Returns a new instance of CacheWarmer.



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
# File 'lib/svelte_on_rails/lib/cache_warmer.rb', line 13

def initialize

  raise "the cache warmer is CURRENTLY INACTIVE!\nNot used currently, but may be useful in the future together with in-memory process-local caching."

  @configs = SvelteOnRails::Configuration.instance.configs

  return unless @configs[:cache_warmer]

  @requests = if File.exist?(file_path)
                JSON.parse(File.read(file_path))
              else
                {}
              end
  @config = SvelteOnRails::Configuration.instance.configs
  ttl = if @config[:redis_cache_store] && @config[:redis_cache_store][:expires_in].present?
          @config[:redis_cache_store][:expires_in]
        end
  ttl ||= 90.minutes
  @ttl = ttl.to_i

  cleanup_requests

  warm_up unless @warmed_up

end

Instance Attribute Details

#requestsObject

Returns the value of attribute requests.



5
6
7
# File 'lib/svelte_on_rails/lib/cache_warmer.rb', line 5

def requests
  @requests
end

Class Method Details

.instanceObject



7
8
9
10
11
# File 'lib/svelte_on_rails/lib/cache_warmer.rb', line 7

def self.instance
  raise "the cache warmer is CURRENTLY INACTIVE!\nNot used currently, but may be useful in the future together with in-memory process-local caching."

  @instance ||= new
end

Instance Method Details

#store_request(component_name, props, html_options, options, view_dir) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/svelte_on_rails/lib/cache_warmer.rb', line 39

def store_request(component_name, props, html_options, options, view_dir)
  req = {
    'component_name' => component_name,
    'props' => props,
    'html_options' => html_options,
    'options' => options,
    'view_dir' => view_dir,
    'timestamp' => Time.now.to_i
  }
  req_key = "#{component_name}@#{view_dir}"
  @requests[req_key] = req

  cleanup_requests

  FileUtils.mkdir_p(File.dirname(file_path))
  File.write(file_path, @requests.to_json)
end

#warm_upObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/svelte_on_rails/lib/cache_warmer.rb', line 57

def warm_up
  success = 0
  errors = []
  support = SvelteOnRails::Lib::ViewHelperSupport

  @requests.each do |k, request|
    begin
      support.new(
        request['component_name'],
        request['props'],
        request['html_options'],
        request['options'].symbolize_keys,
        :cache_warmer,
        request['view_dir']
      )
      success += 1
    rescue => e
      errors.push(e)
    end
  end

  if errors.present?
    puts <<~TEXT
      [SOR] #{success} Components Caches successfully warmed up!
      #{errors.length} Components could not be loaded, Errors:
      +++
#{errors.join("\n  • ")}
      +++
    TEXT
  else
    puts "[SOR] #{success} Components Caches successfully warmed up!"
  end
  @warmed_up = true
end