From 8df6b7cec87885ad36022feb73467c86e3fbee36 Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Wed, 8 Jun 2016 15:21:08 +0200 Subject: [PATCH] Setting model should use Rails.cache instead of class variable #8222 --- app/models/setting.rb | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/app/models/setting.rb b/app/models/setting.rb index 42b179c..218b38b 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -84,10 +84,6 @@ class Setting < ActiveRecord::Base } attr_protected :id - # Hash used to cache setting values - @cached_settings = {} - @cached_cleared_on = Time.now - def value v = read_attribute(:value) # Unserialize serialized settings @@ -106,14 +102,13 @@ class Setting < ActiveRecord::Base # Returns the value of the setting named name def self.[](name) - v = @cached_settings[name] - v ? v : (@cached_settings[name] = find_or_default(name).value) + Rails.cache.fetch("redmine-settings/#{name}") { find_or_default(name).value } end def self.[]=(name, v) setting = find_or_default(name) setting.value = (v ? v : "") - @cached_settings[name] = nil + Rails.cache.delete "redmine-settings/#{name}" setting.save setting.value end @@ -201,15 +196,16 @@ class Setting < ActiveRecord::Base # Called once per request def self.check_cache settings_updated_on = Setting.maximum(:updated_on) - if settings_updated_on && @cached_cleared_on <= settings_updated_on + settings_cleared_on = Rails.cache.read('redmine-settings-cleared-on') + if settings_updated_on && ( settings_cleared_on.blank? || ( settings_cleared_on <= settings_updated_on ) ) clear_cache end end # Clears the settings cache def self.clear_cache - @cached_settings.clear - @cached_cleared_on = Time.now + Rails.cache.delete_matched( /^redmine-settings\/.+$/ ) + Rails.cache.write('redmine-settings-cleared-on', Time.now) logger.info "Settings cache cleared." if logger end -- 2.8.0