Skip to content

Commit af9d5ff

Browse files
committed
Merge pull request #70 from prograils/optimizations
Wrap store_item calls and find_localization body in transactions
2 parents 0f78cb2 + 30faf0d commit af9d5ff

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

Diff for: lib/lit/cache.rb

+30-27
Original file line numberDiff line numberDiff line change
@@ -178,40 +178,43 @@ def localization_keys
178178

179179
def find_localization(locale, key_without_locale, value = nil, force_array = false, update_value = false)
180180
unless value.is_a?(Hash)
181-
localization_key = find_localization_key(key_without_locale)
182-
localization = Lit::Localization.where(locale_id: locale.id).
183-
where(localization_key_id: localization_key.id).first_or_initialize
184-
if update_value || localization.new_record?
185-
if value.is_a?(Array)
186-
unless force_array
187-
new_value = nil
188-
value_clone = value.dup
189-
while (v = value_clone.shift) && v.present?
190-
pv = parse_value(v, locale)
191-
new_value = pv unless pv.nil?
181+
ActiveRecord::Base.transaction do
182+
localization_key = find_localization_key(key_without_locale)
183+
localization = Lit::Localization.where(locale_id: locale.id).
184+
where(localization_key_id: localization_key.id).first_or_initialize
185+
if update_value || localization.new_record?
186+
if value.is_a?(Array)
187+
unless force_array
188+
new_value = nil
189+
value_clone = value.dup
190+
while (v = value_clone.shift) && v.present?
191+
pv = parse_value(v, locale)
192+
new_value = pv unless pv.nil?
193+
end
194+
value = new_value
192195
end
193-
value = new_value
196+
else
197+
value = parse_value(value, locale) unless value.nil?
194198
end
195-
else
196-
value = parse_value(value, locale) unless value.nil?
197-
end
198-
if value.nil?
199-
if Lit.fallback
200-
@locale_cache.keys.each do |lc|
201-
if lc != locale.locale
202-
nk = "#{lc}.#{key_without_locale}"
203-
v = localizations[nk]
204-
value = v if v.present? && value.nil?
199+
if value.nil?
200+
if Lit.fallback
201+
@locale_cache.keys.each do |lc|
202+
if lc != locale.locale
203+
nk = "#{lc}.#{key_without_locale}"
204+
v = localizations[nk]
205+
value = v if v.present? && value.nil?
206+
end
205207
end
206208
end
209+
if value.nil? && Lit.humanize_key
210+
value = key_without_locale.split('.').last.humanize
211+
end
207212
end
208-
if value.nil? && Lit.humanize_key
209-
value = key_without_locale.split('.').last.humanize
210-
end
213+
localization.update_default_value(value)
211214
end
212-
localization.update_default_value(value)
215+
return localization
216+
213217
end
214-
return localization
215218
else
216219
nil
217220
end

Diff for: lib/lit/i18n_backend.rb

+11-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ def reset_available_locales_cache
4141
# @param [Hash] data nested key-value pairs to be added as blurbs
4242
def store_translations(locale, data, options = {})
4343
super
44-
store_item(locale, data) if store_items? && valid_locale?(locale)
44+
ActiveRecord::Base.transaction do
45+
store_item(locale, data)
46+
end if store_items? && valid_locale?(locale)
4547
end
4648

4749
private
@@ -83,9 +85,11 @@ def lookup(locale, key, scope = [], options = {})
8385

8486
def store_item(locale, data, scope = [])
8587
if data.respond_to?(:to_hash)
86-
data.to_hash.each do |key, value|
87-
store_item(locale, value, scope + [key])
88-
end
88+
# ActiveRecord::Base.transaction do
89+
data.to_hash.each do |key, value|
90+
store_item(locale, value, scope + [key])
91+
end
92+
# end
8993
elsif data.respond_to?(:to_str)
9094
key = ([locale] + scope).join('.')
9195
@cache[key] ||= data
@@ -96,8 +100,10 @@ def store_item(locale, data, scope = [])
96100
end
97101

98102
def load_translations_to_cache
99-
(@translations || {}).each do |locale, data|
103+
ActiveRecord::Base.transaction do
104+
(@translations || {}).each do |locale, data|
100105
store_item(locale, data) if valid_locale?(locale)
106+
end
101107
end
102108
end
103109

0 commit comments

Comments
 (0)