FIX: Use a distrbuted cache for site locale.

This commit is contained in:
Guo Xiang Tan 2017-09-12 19:36:17 +08:00
parent 51e1c0b6ce
commit 07bfe3d053
2 changed files with 17 additions and 11 deletions

View File

@ -9,13 +9,14 @@ class SiteSettings::DefaultsProvider
DEFAULT_LOCALE = 'en'.freeze DEFAULT_LOCALE = 'en'.freeze
DEFAULT_CATEGORY = 'required'.freeze DEFAULT_CATEGORY = 'required'.freeze
@@site_locales ||= DistributedCache.new('site_locales')
def initialize(site_setting) def initialize(site_setting)
@site_setting = site_setting @site_setting = site_setting
@site_setting.refresh_settings << DEFAULT_LOCALE_KEY @site_setting.refresh_settings << DEFAULT_LOCALE_KEY
@defaults = {} @defaults = {}
@defaults[DEFAULT_LOCALE.to_sym] = {} @defaults[DEFAULT_LOCALE.to_sym] = {}
@site_locales = {}
refresh_site_locale! refresh_site_locale!
end end
@ -61,21 +62,21 @@ class SiteSettings::DefaultsProvider
end end
def site_locale def site_locale
@site_locales[current_db] @@site_locales[current_db]
end end
def site_locale=(val) def site_locale=(val)
val = val.to_s val = val.to_s
raise Discourse::InvalidParameters.new(:value) unless LocaleSiteSetting.valid_value?(val) 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]) @site_setting.provider.save(DEFAULT_LOCALE_KEY, val, SiteSetting.types[:string])
refresh_site_locale! refresh_site_locale!
@site_setting.refresh! @site_setting.refresh!
Discourse.request_refresh! Discourse.request_refresh!
end end
@site_locales[current_db] @@site_locales[current_db]
end end
def each(&block) def each(&block)
@ -92,7 +93,7 @@ class SiteSettings::DefaultsProvider
description: @site_setting.description(DEFAULT_LOCALE_KEY), description: @site_setting.description(DEFAULT_LOCALE_KEY),
type: SiteSetting.types[SiteSetting.types[:enum]], type: SiteSetting.types[SiteSetting.types[:enum]],
preview: nil, preview: nil,
value: @site_locales[current_db], value: @@site_locales[current_db],
valid_values: LocaleSiteSetting.values, valid_values: LocaleSiteSetting.values,
translate_names: LocaleSiteSetting.translate_names? translate_names: LocaleSiteSetting.translate_names?
} }
@ -100,7 +101,7 @@ class SiteSettings::DefaultsProvider
def refresh_site_locale! def refresh_site_locale!
RailsMultisite::ConnectionManagement.each_connection do |db| RailsMultisite::ConnectionManagement.each_connection do |db|
@site_locales[db] = @@site_locales[db] =
if GlobalSetting.respond_to?(DEFAULT_LOCALE_KEY) && if GlobalSetting.respond_to?(DEFAULT_LOCALE_KEY) &&
(global_val = GlobalSetting.send(DEFAULT_LOCALE_KEY)) && (global_val = GlobalSetting.send(DEFAULT_LOCALE_KEY)) &&
!global_val.blank? !global_val.blank?
@ -111,7 +112,7 @@ class SiteSettings::DefaultsProvider
DEFAULT_LOCALE DEFAULT_LOCALE
end end
@site_locales[db] @@site_locales[db]
end end
end end

View File

@ -2,8 +2,7 @@ require 'rails_helper'
require_dependency 'site_settings/defaults_provider' require_dependency 'site_settings/defaults_provider'
describe SiteSettings::DefaultsProvider do describe SiteSettings::DefaultsProvider do
let(:provider_local) do
let :provider_local do
SiteSettings::LocalProcessProvider.new SiteSettings::LocalProcessProvider.new
end end
@ -14,7 +13,7 @@ describe SiteSettings::DefaultsProvider do
end end
end end
let :settings do let(:settings) do
new_settings(provider_local) new_settings(provider_local)
end end
@ -149,9 +148,15 @@ describe SiteSettings::DefaultsProvider do
end end
describe '.site_locale=' do 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 it 'changes and store the current site locale' do
settings.defaults.site_locale = 'zh_CN' 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 end
it 'changes and store the current site locale' do it 'changes and store the current site locale' do