我们有一个Rails 3.2网站,它相当大,有数千个URL。我们为俄罗斯玩偶缓存实现了Cache_Digests gem。它工作得很好。我们希望通过隔夜预热缓存来进一步优化,以便用户在白天获得更好的体验。我已经看到了这个问题的答案:Rails: Scheduled task to warm up the cache?
是否可以对其进行修改以预热大量URL?
发布于 2015-04-08 17:44:49
要使用昂贵的加载时间为许多页面触发缓存命中,只需创建一个rake任务,以迭代方式将web请求发送到站点内的所有记录/url组合。(Here is one implementation)
迭代Net::HTTP请求所有站点网址/记录:
要只访问每个页面,您可以运行一个nightly任务,以确保清晨的用户仍然有一个包含刷新内容的快速页面。
lib/任务/page.rake_tasks_page.rake
namespace :visit_every_page do
include Net
include Rails.application.routes.url_helpers
task :specializations => :environment do
puts "Visiting specializations..."
Specialization.all.sort{ |a,b| a.id <=> b.id }.each do |s|
begin
puts "Specialization #{s.id}"
City.all.sort{ |a,b| a.id <=> b.id }.each do |c|
puts "Specialization City #{c.id}"
Net::HTTP.get( URI("http://#{APP_CONFIG[:domain]}/specialties/#{s.id}/#{s.token}/refresh_city_cache/#{c.id}.js") )
end
Division.all.sort{ |a,b| a.id <=> b.id }.each do |d|
puts "Specialization Division #{d.id}"
Net::HTTP.get( URI("http://#{APP_CONFIG[:domain]}/specialties/#{s.id}/#{s.token}/refresh_division_cache/#{d.id}.js") )
end
end
end
end
# The following methods are defined to fake out the ActionController
# requirements of the Rails cache
def cache_store
ActionController::Base.cache_store
end
def self.benchmark( *params )
yield
end
def cache_configured?
true
end
end通过自定义控制器操作:
如果您需要绕过用户身份验证限制才能访问您的页面,并且/或者您不想(严重地)搞砸您网站的跟踪分析,您可以创建一个用于命中该use tokens to bypass authentication的缓存摘要的custom controller action
app/controllers/specializations.rb:
class SpecializationsController < ApplicationController
...
before_filter :check_token, :only => [:refresh_cache, :refresh_city_cache, :refresh_division_cache]
skip_authorization_check :only => [:refresh_cache, :refresh_city_cache, :refresh_division_cache]
...
def refresh_cache
@specialization = Specialization.find(params[:id])
@feedback = FeedbackItem.new
render :show, :layout => 'ajax'
end
def refresh_city_cache
@specialization = Specialization.find(params[:id])
@city = City.find(params[:city_id])
render 'refresh_city.js'
end
def refresh_division_cache
@specialization = Specialization.find(params[:id])
@division = Division.find(params[:division_id])
render 'refresh_division.js'
end
end我们的自定义控制器操作渲染其他页面的视图,这会导致对这些页面的缓存命中。例如,refresh_cache呈现与controller#show相同的视图页面和数据,因此对refresh_cache的请求将为这些记录预热与controller#show相同的缓存摘要。
安全说明:
出于安全原因,我建议在提供对任何自定义refresh_cache控制器请求的访问之前,先传递一个令牌and check it,以确保在提供访问之前(如上所述)将URL令牌与数据库记录匹配的it corresponds with a unique token for that record.是微不足道的,因为您的Rake任务可以访问每个记录的惟一令牌--只需在每个请求中传递记录的令牌。
tl;dr:
要触发数千个站点url的/缓存摘要,请创建一个rake任务来迭代请求站点中的每个记录/url组合。您可以通过创建通过令牌进行访问身份验证的自定义控制器操作来绕过此任务的应用程序的用户身份验证限制。
发布于 2018-06-08 09:58:41
我意识到这个问题已经有一年之久了,但我只是在搜索了一堆部分或错误的解决方案后,才找到了我自己的答案。
希望这能帮助下一个人..。
根据我自己的实用程序类,可以在这里找到:https://raw.githubusercontent.com/JayTeeSF/cmd_notes/master/automated_action_runner.rb
你可以简单地运行它(根据它的.help方法)并预缓存你的页面,而不会在这个过程中占用你自己的web服务器。
class AutomatedActionRunner
class StatusObject
def initialize(is_valid, error_obj)
@is_valid = !! is_valid
@error_obj = error_obj
end
def valid?
@is_valid
end
def error
@error_obj
end
end
def self.help
puts <<-EOH
Instead tying-up the frontend of your production site with:
`curl http://your_production_site.com/some_controller/some_action/1234`
`curl http://your_production_site.com/some_controller/some_action/4567`
Try:
`rails r 'AutomatedActionRunner.run(SomeController, "some_action", [{id: "1234"}, {id: "4567"}])'`
EOH
end
def self.common_env
{"rack.input" => "", "SCRIPT_NAME" => "", "HTTP_HOST" => "localhost:3000" }
end
REQUEST_ENV = common_env.freeze
def self.run(controller, controller_action, params_ary=[], user_obj=nil)
success_objects = []
error_objects = []
autorunner = new(controller, controller_action, user_obj)
Rails.logger.warn %Q|[AutomatedAction Kickoff]: Preheating cache for #{params_ary.size} #{autorunner.controller.name}##{controller_action} pages.|
params_ary.each do |params_hash|
status = autorunner.run(params_hash)
if status.valid?
success_objects << params_hash
else
error_objects << status.error
end
end
return process_results(success_objects, error_objects, user_obj.try(:id), autorunner.controller.name, controller_action)
end
def self.process_results(success_objects=[], error_objects=[], user_id, controller_name, controller_action)
message = %Q|AutomatedAction Summary|
backtrace = (error_objects.first.try(:backtrace)||[]).join("\n\t").inspect
num_errors = error_objects.size
num_successes = success_objects.size
log_message = %Q|[#{message}]: Generated #{num_successes} #{controller_name}##{controller_action}, pages; Failed #{num_errors} times; 1st Fail: #{backtrace}|
Rails.logger.warn log_message
# all the local-variables above, are because I typically call Sentry or something with extra parameters!
end
attr_reader :controller
def initialize(controller, controller_action, user_obj)
@controller = controller
@controller = controller.constantize unless controller.respond_to?(:name)
@controller_instance = @controller.new
@controller_action = controller_action
@env_obj = REQUEST_ENV.dup
@user_obj = user_obj
end
def run(params_hash)
Rails.logger.warn %Q|[AutomatedAction]: #{@controller.name}##{@controller_action}(#{params_hash.inspect})|
extend_with_autorun unless @controller_instance.respond_to?(:autorun)
@controller_instance.autorun(@controller_action, params_hash, @env_obj, @user_obj)
end
private
def extend_with_autorun
def @controller_instance.autorun(action_name, action_params, action_env, current_user_value=nil)
self.params = action_params # suppress strong parameters exception
self.request = ActionDispatch::Request.new(action_env)
self.response = ActionDispatch::Response.new
define_singleton_method(:current_user, -> { current_user_value })
send(action_name) # do it
return StatusObject.new(true, nil)
rescue Exception => e
return StatusObject.new(false, e)
end
end
endhttps://stackoverflow.com/questions/29301780
复制相似问题