diff options
author | Jason Lee <huacnlee@gmail.com> | 2015-11-12 17:19:03 +0800 |
---|---|---|
committer | Jason Lee <huacnlee@gmail.com> | 2015-11-12 17:19:03 +0800 |
commit | 2e4a673cbc1a43532e8aa096e4ab5ca034b804f7 (patch) | |
tree | 48944305445797f8eb72ec01b7621e54b71c8e29 | |
parent | ca25289b78e1b49148438831ad6bf165fa0ce56e (diff) | |
download | gitlab-ce-2e4a673cbc1a43532e8aa096e4ab5ca034b804f7.tar.gz |
Add caching for ApplicationSetting, Ci::ApplicationSetting.
ApplicationSetting.current was called in every pages, cache it and expires it after it updated.
This changes will avoid a SQL query in every pages (~0.3 - 0.5ms).
```SQL
SELECT "application_settings".* FROM "application_settings" ORDER BY "application_settings"."id" DESC LIMIT 1
```
-rw-r--r-- | app/models/application_setting.rb | 8 | ||||
-rw-r--r-- | app/models/ci/application_setting.rb | 10 |
2 files changed, 15 insertions, 3 deletions
diff --git a/app/models/application_setting.rb b/app/models/application_setting.rb index 266045f7afa..5a9e55a95f4 100644 --- a/app/models/application_setting.rb +++ b/app/models/application_setting.rb @@ -68,8 +68,14 @@ class ApplicationSetting < ActiveRecord::Base end end + after_commit do + Rails.cache.write('application_setting.last', self) + end + def self.current - ApplicationSetting.last + Rails.cache.fetch('application_setting.last') do + ApplicationSetting.last + end end def self.create_from_defaults diff --git a/app/models/ci/application_setting.rb b/app/models/ci/application_setting.rb index 0cf496f7d81..4ab3e2dcbb3 100644 --- a/app/models/ci/application_setting.rb +++ b/app/models/ci/application_setting.rb @@ -12,9 +12,15 @@ module Ci class ApplicationSetting < ActiveRecord::Base extend Ci::Model - + + after_commit do + Rails.cache.write('ci_application_setting.last', self) + end + def self.current - Ci::ApplicationSetting.last + Rails.cache.fetch('ci_application_setting.last') do + Ci::ApplicationSetting.last + end end def self.create_from_defaults |