Commit c51a32ac authored by Felix Schäfer's avatar Felix Schäfer

Use Rails.cache to cache application settings. #350

parent 5c8e04db
...@@ -84,10 +84,6 @@ class Setting < ActiveRecord::Base ...@@ -84,10 +84,6 @@ class Setting < ActiveRecord::Base
validates_uniqueness_of :name validates_uniqueness_of :name
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)
...@@ -104,16 +100,15 @@ class Setting < ActiveRecord::Base ...@@ -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 ...@@ -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
......
...@@ -67,7 +67,7 @@ LOREM ...@@ -67,7 +67,7 @@ LOREM
def test_new_with_one_attachment def test_new_with_one_attachment
ActionMailer::Base.deliveries.clear ActionMailer::Base.deliveries.clear
Setting.notified_events << 'document_added' Setting.notified_events = Setting.notified_events.dup << 'document_added'
@request.session[:user_id] = 2 @request.session[:user_id] = 2
set_tmp_attachments_directory set_tmp_attachments_directory
......
...@@ -67,7 +67,7 @@ class NewsControllerTest < ActionController::TestCase ...@@ -67,7 +67,7 @@ class NewsControllerTest < ActionController::TestCase
def test_post_create def test_post_create
ActionMailer::Base.deliveries.clear ActionMailer::Base.deliveries.clear
Setting.notified_events << 'news_added' Setting.notified_events = Setting.notified_events.dup << 'news_added'
@request.session[:user_id] = 2 @request.session[:user_id] = 2
post :create, :project_id => 1, :news => { :title => 'NewsControllerTest', post :create, :project_id => 1, :news => { :title => 'NewsControllerTest',
......
...@@ -33,7 +33,7 @@ class RepositoriesFilesystemControllerTest < ActionController::TestCase ...@@ -33,7 +33,7 @@ class RepositoriesFilesystemControllerTest < ActionController::TestCase
@request = ActionController::TestRequest.new @request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new @response = ActionController::TestResponse.new
User.current = nil 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( @repository = Repository::Filesystem.create(
:project => Project.find(PRJ_ID), :project => Project.find(PRJ_ID),
:url => REPOSITORY_PATH, :url => REPOSITORY_PATH,
......
...@@ -33,7 +33,7 @@ class CommentTest < ActiveSupport::TestCase ...@@ -33,7 +33,7 @@ class CommentTest < ActiveSupport::TestCase
end end
def test_create_should_send_notification 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) Watcher.create!(:watchable => @news, :user => @jsmith)
assert_difference 'ActionMailer::Base.deliveries.size' do assert_difference 'ActionMailer::Base.deliveries.size' do
......
...@@ -27,7 +27,7 @@ class DocumentTest < ActiveSupport::TestCase ...@@ -27,7 +27,7 @@ class DocumentTest < ActiveSupport::TestCase
def test_create_should_send_email_notification def test_create_should_send_email_notification
ActionMailer::Base.deliveries.clear 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')) doc = Document.new(:project => Project.find(1), :title => 'New document', :category => Enumeration.find_by_name('User documentation'))
assert doc.save assert doc.save
......
...@@ -30,7 +30,7 @@ class NewsTest < ActiveSupport::TestCase ...@@ -30,7 +30,7 @@ class NewsTest < ActiveSupport::TestCase
def test_create_should_send_email_notification def test_create_should_send_email_notification
ActionMailer::Base.deliveries.clear 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) news = Project.find(:first).news.new(valid_news)
assert news.save assert news.save
......
...@@ -25,7 +25,7 @@ class RepositoryFilesystemTest < ActiveSupport::TestCase ...@@ -25,7 +25,7 @@ class RepositoryFilesystemTest < ActiveSupport::TestCase
def setup def setup
@project = Project.find(3) @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( assert @repository = Repository::Filesystem.create(
:project => @project, :url => REPOSITORY_PATH) :project => @project, :url => REPOSITORY_PATH)
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment