ソースを参照

[rails] Warmup cache before starting up (#9691)

Also make sure records are always loaded from cache for cached_query.
Petrik de Heus 5 ヶ月 前
コミット
9e7dd42ce9

+ 3 - 2
frameworks/Ruby/rails/app/controllers/hello_world_controller.rb

@@ -21,8 +21,9 @@ class HelloWorldController < ActionController::API
   end
 
   def cached_query
-    items = Rails.cache.fetch_multi(*ALL_IDS.sample(query_count)) do |id|
-      World.find(id).as_json
+    keys = ALL_IDS.sample(query_count).map { "world_#{_1}" }
+    items = Rails.cache.fetch_multi(*keys) do |id|
+      raise "Could not find World with id: #{id} in cache"
     end
 
     render json: items.values

+ 6 - 0
frameworks/Ruby/rails/config/application.rb

@@ -47,5 +47,11 @@ module Hello
     config.middleware.delete Rails::Rack::Logger
 
     config.active_support.isolation_level = :fiber if defined?(Falcon)
+
+    config.to_prepare do
+      HelloWorldController::ALL_IDS.each do |id|
+        Rails.cache.write("world_#{id}", World.find(id).as_json, expires_in: 1.day)
+      end
+    end
   end
 end