Skip to content

A platform for community discussion. Free, open, simple.

FIX: Also show localized categories for users who are not logged in (#32460)

Natalie Tay5a4f3b3ab8c8
diff --git a/app/models/site.rb b/app/models/site.rb
index ef84c62b706..61685822438 100644
--- a/app/models/site.rb
+++ b/app/models/site.rb
@@ -232,12 +232,23 @@ class Site
end
seq = nil
+ use_localized_anon_cache = SiteSetting.experimental_content_localization && guardian.anonymous?
+
+ locale = I18n.locale
+ cache_key = "site_json"
+ seq_key = "site_json_seq"
+ version_key = "site_json_version"
+
+ if use_localized_anon_cache
+ cache_key += "_#{locale}"
+ seq_key += "_#{locale}"
+ version_key += "_#{locale}"
+ end
if guardian.anonymous?
seq = MessageBus.last_id("/site_json")
-
cached_json, cached_seq, cached_version =
- Discourse.redis.mget("site_json", "site_json_seq", "site_json_version")
+ Discourse.redis.mget(cache_key, seq_key, version_key)
if cached_json && seq == cached_seq.to_i && Discourse.git_version == cached_version
return cached_json
@@ -249,9 +260,9 @@ class Site
if guardian.anonymous?
Discourse.redis.multi do |transaction|
- transaction.setex "site_json", 1800, json
- transaction.set "site_json_seq", seq
- transaction.set "site_json_version", Discourse.git_version
+ transaction.setex cache_key, 1800, json
+ transaction.set seq_key, seq
+ transaction.set version_key, Discourse.git_version
end
end
@@ -261,9 +272,26 @@ class Site
SITE_JSON_CHANNEL = "/site_json"
def self.clear_anon_cache!
- # publishing forces the sequence up
- # the cache is validated based on the sequence
+ # invalidate both the legacy and localized caches if necessary
MessageBus.publish(SITE_JSON_CHANNEL, "")
+
+ # always clear legacy cache keys
+ Discourse.redis.del("site_json", "site_json_seq", "site_json_version")
+
+ if SiteSetting.experimental_content_localization
+ locales =
+ begin
+ # try to guess which locales might be used
+ I18n.available_locales
+ rescue StandardError
+ # fallback to default locale
+ [I18n.default_locale]
+ end
+
+ locales.each do |loc|
+ Discourse.redis.del("site_json_#{loc}", "site_json_seq_#{loc}", "site_json_version_#{loc}")
+ end
+ end
end
def self.full_name_required_for_signup
diff --git a/spec/models/site_spec.rb b/spec/models/site_spec.rb
index b369443db3c..4f48f0e38ba 100644
--- a/spec/models/site_spec.rb
+++ b/spec/models/site_spec.rb
@@ -373,4 +373,74 @@ RSpec.describe Site do
end
end
end
+
+ context "when there are anonymous users with different locales" do
+ let(:anon_guardian) { Guardian.new }
+ let!(:original_available_locales) { I18n.available_locales }
+ let(:original_locale) { I18n.locale }
+
+ before do
+ SiteSetting.login_required = false
+ Discourse.redis.flushdb
+ I18n.available_locales = %i[en ja]
+ I18n.locale = :en
+ end
+
+ after do
+ I18n.available_locales = original_available_locales
+ I18n.locale = original_locale
+ end
+
+ context "when experimental_content_localization is disabled" do
+ before { SiteSetting.experimental_content_localization = false }
+
+ it "caches anon site json with a global key (not locale scoped)" do
+ expect(Discourse.redis.get("site_json")).to be_nil
+
+ json = Site.json_for(anon_guardian)
+
+ expect(Discourse.redis.get("site_json")).to eq(json)
+
+ I18n.locale = :ja
+ json_ja = Site.json_for(anon_guardian)
+ expect(Discourse.redis.get("site_json")).to eq(json_ja)
+
+ # always overwritten, not per locale
+ I18n.locale = :en
+ expect(Discourse.redis.get("site_json")).to eq(json)
+ end
+ end
+
+ context "when experimental_content_localization is enabled" do
+ before { SiteSetting.experimental_content_localization = true }
+
+ it "caches anon site json separately for each locale" do
+ expect(Discourse.redis.get("site_json_en")).to be_nil
+ expect(Discourse.redis.get("site_json_ja")).to be_nil
+
+ json_en = Site.json_for(anon_guardian)
+ expect(Discourse.redis.get("site_json_en")).to eq(json_en)
+
+ I18n.locale = :ja
+ json_ja = Site.json_for(anon_guardian)
+ expect(Discourse.redis.get("site_json_ja")).to eq(json_ja)
+
+ expect(json_en).not_to eq(json_ja)
+ end
+
+ it "clears all locale caches on clear_anon_cache!" do
+ I18n.locale = :en
+ Site.json_for(anon_guardian)
+ I18n.locale = :ja
+ Site.json_for(anon_guardian)
+
+ expect(Discourse.redis.get("site_json_en")).to be_present
+ expect(Discourse.redis.get("site_json_ja")).to be_present
+
+ described_class.clear_anon_cache!
+ expect(Discourse.redis.get("site_json_en")).to be_nil
+ expect(Discourse.redis.get("site_json_ja")).to be_nil
+ end
+ end
+ end
end