|
|
@ -85,10 +85,6 @@ class Setting < ActiveRecord::Base |
|
|
|
validates_inclusion_of :name, :in => @@available_settings.keys |
|
|
|
validates_inclusion_of :name, :in => @@available_settings.keys |
|
|
|
validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' } |
|
|
|
validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' } |
|
|
|
|
|
|
|
|
|
|
|
# Hash used to cache setting values |
|
|
|
|
|
|
|
@cached_settings = {} |
|
|
|
|
|
|
|
@cached_cleared_on = Time.now |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def value |
|
|
|
def value |
|
|
|
v = read_attribute(:value) |
|
|
|
v = read_attribute(:value) |
|
|
|
# Unserialize serialized settings |
|
|
|
# Unserialize serialized settings |
|
|
@ -104,16 +100,15 @@ class Setting < ActiveRecord::Base |
|
|
|
|
|
|
|
|
|
|
|
# Returns the value of the setting named name |
|
|
|
# Returns the value of the setting named name |
|
|
|
def self.[](name) |
|
|
|
def self.[](name) |
|
|
|
v = @cached_settings[name] |
|
|
|
Marshal.load(Rails.cache.fetch("chiliproject/setting/#{name}") {Marshal.dump(find_or_default(name).value)}).freeze |
|
|
|
v ? v : (@cached_settings[name] = find_or_default(name).value) |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def self.[]=(name, v) |
|
|
|
def self.[]=(name, v) |
|
|
|
setting = find_or_default(name) |
|
|
|
setting = find_or_default(name) |
|
|
|
setting.value = (v ? v : "") |
|
|
|
setting.value = (v ? v : "") |
|
|
|
@cached_settings[name] = nil |
|
|
|
Rails.cache.delete "chiliproject/setting/#{name}" |
|
|
|
setting.save |
|
|
|
setting.save |
|
|
|
setting.value |
|
|
|
setting.value.freeze |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
# Defines getter and setter for each setting |
|
|
|
# Defines getter and setter for each setting |
|
|
@ -150,10 +145,12 @@ class Setting < ActiveRecord::Base |
|
|
|
# Called once per request |
|
|
|
# Called once per request |
|
|
|
def self.check_cache |
|
|
|
def self.check_cache |
|
|
|
settings_updated_on = Setting.maximum(:updated_on) |
|
|
|
settings_updated_on = Setting.maximum(:updated_on) |
|
|
|
if settings_updated_on && @cached_cleared_on <= settings_updated_on |
|
|
|
cache_cleared_on = Rails.cache.read('chiliproject/setting-cleared_on') |
|
|
|
@cached_settings.clear |
|
|
|
cache_cleared_on = cache_cleared_on ? Marshal.load(cache_cleared_on) : Time.now |
|
|
|
@cached_cleared_on = Time.now |
|
|
|
if settings_updated_on && cache_cleared_on <= settings_updated_on |
|
|
|
logger.info "Settings cache cleared." if logger |
|
|
|
Rails.cache.delete_matched( /^chiliproject\/setting\/.+$/ ) |
|
|
|
|
|
|
|
Rails.cache.write('chiliproject/setting-cleared_on', Marshal.dump(Time.now)) |
|
|
|
|
|
|
|
logger.info 'Settings cache cleared.' if logger |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|