mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
PERF: simplify and shrink the translation cache
FIX: leaking objects into the translation cache causing sidekiq to grow
This commit is contained in:
parent
586cca352d
commit
830ce05fe6
3 changed files with 15 additions and 22 deletions
|
@ -25,8 +25,9 @@ class PostActionTypeSerializer < ApplicationSerializer
|
|||
|
||||
protected
|
||||
|
||||
def i18n(field, vars={})
|
||||
I18n.t("post_action_types.#{object.name_key}.#{field}", vars)
|
||||
def i18n(field, vars=nil)
|
||||
key = "post_action_types.#{object.name_key}.#{field}"
|
||||
vars ? I18n.t(key, vars) : I18n.t(key)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -2,8 +2,9 @@ class TopicFlagTypeSerializer < PostActionTypeSerializer
|
|||
|
||||
protected
|
||||
|
||||
def i18n(field, vars={})
|
||||
I18n.t("topic_flag_types.#{object.name_key}.#{field}", vars)
|
||||
def i18n(field, vars=nil)
|
||||
key = "topic_flag_types.#{object.name_key}.#{field}"
|
||||
vars ? I18n.t(key,vars) : I18n.t(key)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -34,7 +34,7 @@ module I18n
|
|||
class << self
|
||||
alias_method :translate_no_cache, :translate
|
||||
alias_method :reload_no_cache!, :reload!
|
||||
LRU_CACHE_SIZE = 2000
|
||||
LRU_CACHE_SIZE = 300
|
||||
|
||||
def reload!
|
||||
@loaded_locales = []
|
||||
|
@ -59,26 +59,17 @@ module I18n
|
|||
end
|
||||
end
|
||||
|
||||
def translate(*args)
|
||||
@cache ||= LruRedux::ThreadSafeCache.new(LRU_CACHE_SIZE)
|
||||
found = true
|
||||
k = [args, config.locale, config.backend.object_id]
|
||||
t = @cache.fetch(k) { found = false }
|
||||
unless found
|
||||
def translate(key, *args)
|
||||
load_locale(config.locale) unless @loaded_locales.include?(config.locale)
|
||||
begin
|
||||
t = translate_no_cache(*args)
|
||||
rescue MissingInterpolationArgument
|
||||
options = args.last.is_a?(Hash) ? args.pop.dup : {}
|
||||
options.merge!(locale: config.default_locale)
|
||||
key = args.shift
|
||||
t = translate_no_cache(key, options)
|
||||
ensure
|
||||
t = @cache[k] = t.freeze
|
||||
return translate_no_cache(key, *args) if args.length > 0
|
||||
|
||||
@cache ||= LruRedux::ThreadSafeCache.new(LRU_CACHE_SIZE)
|
||||
k = "#{key}#{config.locale}#{config.backend.object_id}"
|
||||
|
||||
@cache.getset(k) do
|
||||
translate_no_cache(key).freeze
|
||||
end
|
||||
end
|
||||
t
|
||||
end
|
||||
|
||||
alias_method :t, :translate
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue