PERF: Cache ToS and Privacy Policy paths (#21860)

Checking if the topic exists happened often and that can cause
performance issues.
This commit is contained in:
Bianca Nenciu
2023-06-07 20:31:20 +02:00
committed by GitHub
parent 987ec602ec
commit 5fc1586abf
8 changed files with 64 additions and 32 deletions

View File

@ -597,6 +597,46 @@ module Discourse
alias_method :base_url_no_path, :base_url_no_prefix
end
def self.urls_cache
@urls_cache ||= DistributedCache.new("urls_cache")
end
def self.tos_url
if SiteSetting.tos_url.present?
SiteSetting.tos_url
else
urls_cache["tos"] ||= (
if SiteSetting.tos_topic_id > 0 && Topic.exists?(id: SiteSetting.tos_topic_id)
"#{Discourse.base_path}/tos"
else
:nil
end
)
urls_cache["tos"] != :nil ? urls_cache["tos"] : nil
end
end
def self.privacy_policy_url
if SiteSetting.privacy_policy_url.present?
SiteSetting.privacy_policy_url
else
urls_cache["privacy_policy"] ||= (
if SiteSetting.privacy_topic_id > 0 && Topic.exists?(id: SiteSetting.privacy_topic_id)
"#{Discourse.base_path}/privacy"
else
:nil
end
)
urls_cache["privacy_policy"] != :nil ? urls_cache["privacy_policy"] : nil
end
end
def self.clear_urls!
urls_cache.clear
end
LAST_POSTGRES_READONLY_KEY = "postgres:last_readonly"
READONLY_MODE_KEY_TTL ||= 60