Patch #1321 » 0002-remove-settings-cache-which-only-worked-per-request.patch
| app/controllers/application.rb | ||
|---|---|---|
| 31 | 31 |
end |
| 32 | 32 |
|
| 33 | 33 |
def user_setup |
| 34 |
# Check the settings cache for each request |
|
| 35 |
Setting.check_cache |
|
| 36 | 34 |
# Find the current user |
| 37 | 35 |
User.current = find_current_user |
| 38 | 36 |
end |
| app/models/setting.rb | ||
|---|---|---|
| 50 | 50 |
validates_inclusion_of :name, :in => @@available_settings.keys |
| 51 | 51 |
validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' }
|
| 52 | 52 | |
| 53 |
# Hash used to cache setting values |
|
| 54 |
@cached_settings = {}
|
|
| 55 |
@cached_cleared_on = Time.now |
|
| 56 |
|
|
| 57 | 53 |
def value |
| 58 | 54 |
v = read_attribute(:value) |
| 59 | 55 |
# Unserialize serialized settings |
| ... | ... | |
| 69 | 65 |
|
| 70 | 66 |
# Returns the value of the setting named name |
| 71 | 67 |
def self.[](name) |
| 72 |
v = @cached_settings[name] |
|
| 73 |
v ? v : (@cached_settings[name] = find_or_default(name).value) |
|
| 68 |
find_or_default(name).value |
|
| 74 | 69 |
end |
| 75 | 70 |
|
| 76 | 71 |
def self.[]=(name, v) |
| 77 | 72 |
setting = find_or_default(name) |
| 78 | 73 |
setting.value = (v ? v : "") |
| 79 |
@cached_settings[name] = nil |
|
| 80 | 74 |
setting.save |
| 81 | 75 |
setting.value |
| 82 | 76 |
end |
| ... | ... | |
| 106 | 100 |
per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
|
| 107 | 101 |
end |
| 108 | 102 |
|
| 109 |
# Checks if settings have changed since the values were read |
|
| 110 |
# and clears the cache hash if it's the case |
|
| 111 |
# Called once per request |
|
| 112 |
def self.check_cache |
|
| 113 |
settings_updated_on = Setting.maximum(:updated_on) |
|
| 114 |
if settings_updated_on && @cached_cleared_on <= settings_updated_on |
|
| 115 |
@cached_settings.clear |
|
| 116 |
@cached_cleared_on = Time.now |
|
| 117 |
logger.info "Settings cache cleared." if logger |
|
| 118 |
end |
|
| 119 |
end |
|
| 120 | 103 |
|
| 121 | 104 |
private |
| 122 | 105 |
# Returns the Setting instance for the setting named name |
- « Previous
- 1
- 2
- Next »