Project

General

Profile

Patch #8222 » 0001-Setting-model-should-use-Rails.cache-instead-of-clas.patch

Updated version, addressing bug described in #12 - Gregor Schmidt, 2016-06-08 21:54

View differences:

app/models/setting.rb
84 84
  }
85 85
  attr_protected :id
86 86

  
87
  # Hash used to cache setting values
88
  @cached_settings = {}
89
  @cached_cleared_on = Time.now
90

  
91 87
  def value
92 88
    v = read_attribute(:value)
93 89
    # Unserialize serialized settings
......
106 102

  
107 103
  # Returns the value of the setting named name
108 104
  def self.[](name)
109
    v = @cached_settings[name]
110
    v ? v : (@cached_settings[name] = find_or_default(name).value)
105
    Rails.cache.fetch("redmine-settings/#{name}") { find_or_default(name).value }
111 106
  end
112 107

  
113 108
  def self.[]=(name, v)
114 109
    setting = find_or_default(name)
115 110
    setting.value = (v ? v : "")
116
    @cached_settings[name] = nil
111
    Rails.cache.delete "redmine-settings/#{name}"
117 112
    setting.save
118 113
    setting.value
119 114
  end
......
201 196
  # Called once per request
202 197
  def self.check_cache
203 198
    settings_updated_on = Setting.maximum(:updated_on)
204
    if settings_updated_on && @cached_cleared_on <= settings_updated_on
199
    settings_cleared_on = Rails.cache.read('redmine-settings-cleared-on')
200
    if settings_updated_on && ( settings_cleared_on.blank? || ( settings_cleared_on <= settings_updated_on ) )
205 201
      clear_cache
206 202
    end
207 203
  end
208 204

  
209 205
  # Clears the settings cache
210 206
  def self.clear_cache
211
    @cached_settings.clear
212
    @cached_cleared_on = Time.now
207
    Rails.cache.delete_matched( /^redmine-settings\/.+$/ )
208
    Rails.cache.write('redmine-settings-cleared-on', Time.now)
213 209
    logger.info "Settings cache cleared." if logger
214 210
  end
215 211

  
(3-3/3)