Patch #8222 » 0001-Setting-model-should-use-Rails.cache-instead-of-clas.patch
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 |
if settings_updated_on && (Rails.cache.read('redmine-settings-cleared-on')||Time.now) <= settings_updated_on
|
|
205 | 200 |
clear_cache |
206 | 201 |
end |
207 | 202 |
end |
208 | 203 | |
209 | 204 |
# Clears the settings cache |
210 | 205 |
def self.clear_cache |
211 |
@cached_settings.clear
|
|
212 |
@cached_cleared_on = Time.now
|
|
206 |
Rails.cache.delete_matched( /^redmine-settings\/.+$/ )
|
|
207 |
Rails.cache.write('redmine-settings-cleared-on', Time.now)
|
|
213 | 208 |
logger.info "Settings cache cleared." if logger |
214 | 209 |
end |
215 | 210 |