Use Rails.cache to cache application settings. #350
This commit is contained in:
parent
5c8e04db55
commit
c51a32ac23
|
@ -84,10 +84,6 @@ class Setting < ActiveRecord::Base
|
|||
validates_uniqueness_of :name
|
||||
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' }
|
||||
|
||||
# Hash used to cache setting values
|
||||
@cached_settings = {}
|
||||
@cached_cleared_on = Time.now
|
||||
|
||||
def value
|
||||
v = read_attribute(:value)
|
||||
|
@ -104,16 +100,15 @@ 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)
|
||||
Marshal.load(Rails.cache.fetch("chiliproject/setting/#{name}") {Marshal.dump(find_or_default(name).value)}).freeze
|
||||
end
|
||||
|
||||
def self.[]=(name, v)
|
||||
setting = find_or_default(name)
|
||||
setting.value = (v ? v : "")
|
||||
@cached_settings[name] = nil
|
||||
Rails.cache.delete "chiliproject/setting/#{name}"
|
||||
setting.save
|
||||
setting.value
|
||||
setting.value.freeze
|
||||
end
|
||||
|
||||
# Defines getter and setter for each setting
|
||||
|
@ -150,10 +145,12 @@ 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
|
||||
@cached_settings.clear
|
||||
@cached_cleared_on = Time.now
|
||||
logger.info "Settings cache cleared." if logger
|
||||
cache_cleared_on = Rails.cache.read('chiliproject/setting-cleared_on')
|
||||
cache_cleared_on = cache_cleared_on ? Marshal.load(cache_cleared_on) : Time.now
|
||||
if settings_updated_on && cache_cleared_on <= settings_updated_on
|
||||
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
|
||||
|
||||
|
|
|
@ -67,7 +67,7 @@ LOREM
|
|||
|
||||
def test_new_with_one_attachment
|
||||
ActionMailer::Base.deliveries.clear
|
||||
Setting.notified_events << 'document_added'
|
||||
Setting.notified_events = Setting.notified_events.dup << 'document_added'
|
||||
@request.session[:user_id] = 2
|
||||
set_tmp_attachments_directory
|
||||
|
||||
|
|
|
@ -67,7 +67,7 @@ class NewsControllerTest < ActionController::TestCase
|
|||
|
||||
def test_post_create
|
||||
ActionMailer::Base.deliveries.clear
|
||||
Setting.notified_events << 'news_added'
|
||||
Setting.notified_events = Setting.notified_events.dup << 'news_added'
|
||||
|
||||
@request.session[:user_id] = 2
|
||||
post :create, :project_id => 1, :news => { :title => 'NewsControllerTest',
|
||||
|
|
|
@ -33,7 +33,7 @@ class RepositoriesFilesystemControllerTest < ActionController::TestCase
|
|||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
User.current = nil
|
||||
Setting.enabled_scm << 'Filesystem' unless Setting.enabled_scm.include?('Filesystem')
|
||||
Setting.enabled_scm = Setting.enabled_scm.dup << 'Filesystem' unless Setting.enabled_scm.include?('Filesystem')
|
||||
@repository = Repository::Filesystem.create(
|
||||
:project => Project.find(PRJ_ID),
|
||||
:url => REPOSITORY_PATH,
|
||||
|
|
|
@ -33,7 +33,7 @@ class CommentTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
def test_create_should_send_notification
|
||||
Setting.notified_events << 'news_comment_added'
|
||||
Setting.notified_events = Setting.notified_events.dup << 'news_comment_added'
|
||||
Watcher.create!(:watchable => @news, :user => @jsmith)
|
||||
|
||||
assert_difference 'ActionMailer::Base.deliveries.size' do
|
||||
|
|
|
@ -27,7 +27,7 @@ class DocumentTest < ActiveSupport::TestCase
|
|||
|
||||
def test_create_should_send_email_notification
|
||||
ActionMailer::Base.deliveries.clear
|
||||
Setting.notified_events << 'document_added'
|
||||
Setting.notified_events = Setting.notified_events.dup << 'document_added'
|
||||
doc = Document.new(:project => Project.find(1), :title => 'New document', :category => Enumeration.find_by_name('User documentation'))
|
||||
|
||||
assert doc.save
|
||||
|
|
|
@ -30,7 +30,7 @@ class NewsTest < ActiveSupport::TestCase
|
|||
|
||||
def test_create_should_send_email_notification
|
||||
ActionMailer::Base.deliveries.clear
|
||||
Setting.notified_events << 'news_added'
|
||||
Setting.notified_events = Setting.notified_events.dup << 'news_added'
|
||||
news = Project.find(:first).news.new(valid_news)
|
||||
|
||||
assert news.save
|
||||
|
|
|
@ -25,7 +25,7 @@ class RepositoryFilesystemTest < ActiveSupport::TestCase
|
|||
|
||||
def setup
|
||||
@project = Project.find(3)
|
||||
Setting.enabled_scm << 'Filesystem' unless Setting.enabled_scm.include?('Filesystem')
|
||||
Setting.enabled_scm = Setting.enabled_scm.dup << 'Filesystem' unless Setting.enabled_scm.include?('Filesystem')
|
||||
assert @repository = Repository::Filesystem.create(
|
||||
:project => @project, :url => REPOSITORY_PATH)
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue