考慮以下在application_controller.rb中before_action/filter是一個不好的做法嗎?
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :maintenance_mode
private
def maintenance_mode
@settings = Setting.first
if @settings.maintenance
if logged_in?
if !current_user.admin?
redirect_to maintenance_url
end
else
redirect_to maintenance_url
end
end
end
是否有性能問題或不好的做法,一般的在全球範圍內使用before_actions?所以我創建了一個維護模式,如果在數據庫中維護屬性有一個真正的值(這將在我假設的每個請求上進行檢查),並且它可能不是最好的方法,那麼是否有解決方法?
我可以想象在後臺進程中每分鐘檢查一次cron job/rake任務,但是我真正想知道的是before_action總體上是一件壞事嗎?
我可能會丟失一些東西 - 但是如果我現在正在維護模式下訪問您的網站,現在我的會話總是認爲該網站處於維護模式,這看起來不正確;我需要等待會話超時再次使用該站點。糟糕的用戶體驗會影響性能(可能不需要)。 – house9
我同意你的意見,如果你正在講述的話,將它存儲在會話中是一個錯誤。但還有其他替代方法,如Cookie和緩存可能會過期。如果適用於所有用戶,我更喜歡緩存。 –
@MohamedOsama你如何使用這行'@settings = Rails.cache.fetch {Setting.first}',因爲它表示'參數的數量錯誤(0表示1..2)' –