我有一个调度程序,它通过运行rake任务使缓存无效。
应用/模型/ parameter.rb
class Parameter < ActiveRecord::Base
after_update :invalidate_bliss_config
def invalidate_bliss_config
Parameter.invalidate_config
end
def self.bliss_config
Rails.cache.fetch('bliss_config') { Parameter.first }
end
def self.invalidate_config
Rails.cache.delete('bliss_config')
end
def self.auto_change_event_horizon
Parameter.first.update_attributes!(event_horizon: (Time.zone.today.beginning_of_month + 5.month).end_of_month)
end
end
配置/ schedule.rb
set :output, "#{File.expand_path(File.dirname(__FILE__))}/../log/cron_log.log"
set :rails_root, "#{File.expand_path(File.dirname(__FILE__))}/../"
set :environment, "production"
# Move event horizon on first day of every month.
every '0 0 1 * *' do
command "cd #{rails_root} && rvm use 1.9.3 && rvm gemset use rails_3_2_21 && RAILS_ENV=#{environment} bundle exec rake bliss:update_event_horizon"
end
LIB /任务/ bliss.rake
namespace :bliss do
task :update_event_horizon => :environment do
Parameter.auto_change_event_horizon
end
end
更新
配置/环境/ development.rb
Bliss::Application.configure do
config.cache_classes = false
config.whiny_nils = true
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_mailer.raise_delivery_errors = false
config.active_support.deprecation = :log
config.action_dispatch.best_standards_support = :builtin
config.active_record.mass_assignment_sanitizer = :strict
config.active_record.auto_explain_threshold_in_seconds = 0.5
end
配置/环境/ production.rb
Bliss::Application.configure do
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.action_dispatch.x_sendfile_header = "X-Sendfile"
config.serve_static_assets = false
config.i18n.fallbacks = true
config.assets.precompile += %w(credit_card_track_parser.js register.js fast_autocompleter.js payment.js)
config.active_support.deprecation = :notify
end
在用户界面中,我们使用Parameter.bliss_config.event_horizon
如果我在UI中检查属性值,则运行任务后,它不会返回最新更改。但是,如果我通过rails console
检查它会返回最新值。如果我重新启动服务器,它将采用最新值(预期!)。
P.S。 1.此问题仅在生产中发生。它在开发中完美运行。 2.如果我从UI更改此配置,则需要使用最新值。