From 737bb9673791e677780c6a1682f0a8780e8dd5bb Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 24 Apr 2013 14:40:09 +1000 Subject: [PATCH] translation accelerator --- lib/freedom_patches/translate_accelerator.rb | 27 ++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 lib/freedom_patches/translate_accelerator.rb diff --git a/lib/freedom_patches/translate_accelerator.rb b/lib/freedom_patches/translate_accelerator.rb new file mode 100644 index 000000000..617a44996 --- /dev/null +++ b/lib/freedom_patches/translate_accelerator.rb @@ -0,0 +1,27 @@ +module I18n + # this accelerates translation a tiny bit (halves the time it takes) + class << self + alias_method :translate_no_cache, :translate + alias_method :reload_no_cache!, :reload! + LRU_CACHE_SIZE = 2000 + + def reload! + @cache = nil + reload_no_cache! + 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 + t = @cache[k] = translate_no_cache(*args) + end + + t + end + + alias_method :t, :translate + end +end