From 07bfe3d053814c5691652a5eda9142a86968d510 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 12 Sep 2017 19:36:17 +0800 Subject: [PATCH] FIX: Use a distrbuted cache for site locale. --- lib/site_settings/defaults_provider.rb | 15 ++++++++------- .../site_settings/defaults_provider_spec.rb | 13 +++++++++---- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/site_settings/defaults_provider.rb b/lib/site_settings/defaults_provider.rb index 078a62dc043..7b1467a6535 100644 --- a/lib/site_settings/defaults_provider.rb +++ b/lib/site_settings/defaults_provider.rb @@ -9,13 +9,14 @@ class SiteSettings::DefaultsProvider DEFAULT_LOCALE = 'en'.freeze DEFAULT_CATEGORY = 'required'.freeze + @@site_locales ||= DistributedCache.new('site_locales') + def initialize(site_setting) @site_setting = site_setting @site_setting.refresh_settings << DEFAULT_LOCALE_KEY @defaults = {} @defaults[DEFAULT_LOCALE.to_sym] = {} - @site_locales = {} refresh_site_locale! end @@ -61,21 +62,21 @@ class SiteSettings::DefaultsProvider end def site_locale - @site_locales[current_db] + @@site_locales[current_db] end def site_locale=(val) val = val.to_s raise Discourse::InvalidParameters.new(:value) unless LocaleSiteSetting.valid_value?(val) - if val != @site_locales[current_db] + if val != @@site_locales[current_db] @site_setting.provider.save(DEFAULT_LOCALE_KEY, val, SiteSetting.types[:string]) refresh_site_locale! @site_setting.refresh! Discourse.request_refresh! end - @site_locales[current_db] + @@site_locales[current_db] end def each(&block) @@ -92,7 +93,7 @@ class SiteSettings::DefaultsProvider description: @site_setting.description(DEFAULT_LOCALE_KEY), type: SiteSetting.types[SiteSetting.types[:enum]], preview: nil, - value: @site_locales[current_db], + value: @@site_locales[current_db], valid_values: LocaleSiteSetting.values, translate_names: LocaleSiteSetting.translate_names? } @@ -100,7 +101,7 @@ class SiteSettings::DefaultsProvider def refresh_site_locale! RailsMultisite::ConnectionManagement.each_connection do |db| - @site_locales[db] = + @@site_locales[db] = if GlobalSetting.respond_to?(DEFAULT_LOCALE_KEY) && (global_val = GlobalSetting.send(DEFAULT_LOCALE_KEY)) && !global_val.blank? @@ -111,7 +112,7 @@ class SiteSettings::DefaultsProvider DEFAULT_LOCALE end - @site_locales[db] + @@site_locales[db] end end diff --git a/spec/components/site_settings/defaults_provider_spec.rb b/spec/components/site_settings/defaults_provider_spec.rb index 465e177377d..7587748a340 100644 --- a/spec/components/site_settings/defaults_provider_spec.rb +++ b/spec/components/site_settings/defaults_provider_spec.rb @@ -2,8 +2,7 @@ require 'rails_helper' require_dependency 'site_settings/defaults_provider' describe SiteSettings::DefaultsProvider do - - let :provider_local do + let(:provider_local) do SiteSettings::LocalProcessProvider.new end @@ -14,7 +13,7 @@ describe SiteSettings::DefaultsProvider do end end - let :settings do + let(:settings) do new_settings(provider_local) end @@ -149,9 +148,15 @@ describe SiteSettings::DefaultsProvider do end describe '.site_locale=' do + it 'should store site locale in a distributed cache' do + expect(settings.defaults.class.class_variable_get(:@@site_locales)) + .to be_a(DistributedCache) + end + it 'changes and store the current site locale' do settings.defaults.site_locale = 'zh_CN' - expect(settings.defaults.site_locale).to eq 'zh_CN' + + expect(settings.defaults.site_locale).to eq('zh_CN') end it 'changes and store the current site locale' do