Class: Lit::Cache
- Inherits:
-
Object
- Object
- Lit::Cache
- Defined in:
- lib/lit/cache.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete_key(key) ⇒ Object
- #delete_locale(key) ⇒ Object
-
#export ⇒ Object
this comes directly from copycopter.
- #find_locale(locale_key) ⇒ Object
- #get_global_hits_counter(key) ⇒ Object
- #get_hits_counter(key) ⇒ Object
- #get_request_info(key_without_locale) ⇒ Object
- #has_key?(key) ⇒ Boolean
- #init_key_with_value(key, value) ⇒ Object
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #keys ⇒ Object
- #load_all_translations ⇒ Object
- #nested_string_keys_to_hash(db_localizations) ⇒ Object
- #refresh_key(key) ⇒ Object
- #request_keys ⇒ Object
- #reset ⇒ Object (also: #clear)
- #restore_hits_counter ⇒ Object
- #stop_hits_counter ⇒ Object
- #sync ⇒ Object
- #update_cache(key, value) ⇒ Object
- #update_locale(key, value, force_array = false, startup_process = false) ⇒ Object
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
21 22 23 24 25 26 |
# File 'lib/lit/cache.rb', line 21 def initialize @hits_counter = Lit.get_key_value_engine @request_info_store = Lit.get_key_value_engine @hits_counter_working = true @keys = nil end |
Instance Method Details
#[](key) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/lit/cache.rb', line 28 def [](key) key_without_locale = split_key(key).last update_hits_count(key) store_request_info(key_without_locale) localization = localizations[key] update_request_keys(key_without_locale, localization) localization end |
#[]=(key, value) ⇒ Object
37 38 39 |
# File 'lib/lit/cache.rb', line 37 def []=(key, value) update_locale(key, value) end |
#delete_key(key) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/lit/cache.rb', line 103 def delete_key(key) key = key.to_s localizations.delete(key) key_without_locale = split_key(key).last localization_keys.delete(key_without_locale) I18n.backend.reload! end |
#delete_locale(key) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/lit/cache.rb', line 76 def delete_locale(key) key = key.to_s keys.delete(key) locale_key, key_without_locale = split_key(key) locale = find_locale(locale_key) delete_localization(locale, key_without_locale) end |
#export ⇒ Object
this comes directly from copycopter.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/lit/cache.rb', line 130 def export reset localizations_scope = Lit::Localization unless ENV['LOCALES'].blank? locale_keys = ENV['LOCALES'].to_s.split(',') || [] locale_ids = Lit::Locale.where(locale: locale_keys).pluck(:id) localizations_scope = localizations_scope.where(locale_id: locale_ids) unless locale_ids.empty? end db_localizations = {} localizations_scope.find_each do |l| db_localizations[l.full_key] = l.get_value end exported_keys = nested_string_keys_to_hash(db_localizations) exported_keys.to_yaml end |
#find_locale(locale_key) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/lit/cache.rb', line 119 def find_locale(locale_key) locale_key = locale_key.to_s @locale_cache ||= {} unless @locale_cache.key?(locale_key) locale = Lit::Locale.where(locale: locale_key).first_or_create! @locale_cache[locale_key] = locale end @locale_cache[locale_key] end |
#get_global_hits_counter(key) ⇒ Object
163 164 165 |
# File 'lib/lit/cache.rb', line 163 def get_global_hits_counter(key) @hits_counter['global_hits_counter.' + key] end |
#get_hits_counter(key) ⇒ Object
167 168 169 |
# File 'lib/lit/cache.rb', line 167 def get_hits_counter(key) @hits_counter['hits_counter.' + key] end |
#get_request_info(key_without_locale) ⇒ Object
338 339 340 |
# File 'lib/lit/cache.rb', line 338 def get_request_info(key_without_locale) @request_info_store['request_info.' + key_without_locale].to_s end |
#has_key?(key) ⇒ Boolean
45 46 47 |
# File 'lib/lit/cache.rb', line 45 def has_key?(key) localizations.has_key?(key) end |
#init_key_with_value(key, value) ⇒ Object
41 42 43 |
# File 'lib/lit/cache.rb', line 41 def init_key_with_value(key, value) update_locale(key, value, true) end |
#keys ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/lit/cache.rb', line 53 def keys return @keys if @keys.present? @keys = localizations.keys return @keys if localizations.prefix.nil? @keys = @keys.map do |k| k.gsub(/^#{localizations.prefix}/, '') end end |
#load_all_translations ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/lit/cache.rb', line 84 def load_all_translations first = Localization.order(id: :asc).first last = Localization.order(id: :desc).first if !first || (!localizations.has_key?(first.full_key) || !localizations.has_key?(last.full_key)) Localization.includes([:locale, :localization_key]).find_each do |l| localizations[l.full_key] = l.get_value end end end |
#nested_string_keys_to_hash(db_localizations) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/lit/cache.rb', line 146 def nested_string_keys_to_hash(db_localizations) # http://subtech.g.hatena.ne.jp/cho45/20061122 deep_proc = proc do |_k, s, o| if s.is_a?(Hash) && o.is_a?(Hash) next s.merge(o, &deep_proc) end next o end nested_keys = {} db_localizations.sort.each do |k, v| key_parts = k.to_s.split('.') converted = key_parts.reverse.reduce(v) { |a, n| { n => a } } nested_keys.merge!(converted, &deep_proc) end nested_keys end |
#refresh_key(key) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/lit/cache.rb', line 95 def refresh_key(key) key = key.to_s locale_key, key_without_locale = split_key(key) locale = find_locale(locale_key) localization = find_localization(locale, key_without_locale, default_fallback: true) localizations[key] = localization.get_value if localization end |
#request_keys ⇒ Object
333 334 335 |
# File 'lib/lit/cache.rb', line 333 def request_keys Thread.current[:lit_request_keys] || {} end |
#reset ⇒ Object Also known as: clear
111 112 113 114 115 116 |
# File 'lib/lit/cache.rb', line 111 def reset @locale_cache = {} localizations.clear localization_keys.clear load_all_translations end |
#restore_hits_counter ⇒ Object
175 176 177 |
# File 'lib/lit/cache.rb', line 175 def restore_hits_counter @hits_counter_working = true end |
#stop_hits_counter ⇒ Object
171 172 173 |
# File 'lib/lit/cache.rb', line 171 def stop_hits_counter @hits_counter_working = false end |
#sync ⇒ Object
49 50 51 |
# File 'lib/lit/cache.rb', line 49 def sync localizations.clear end |
#update_cache(key, value) ⇒ Object
71 72 73 74 |
# File 'lib/lit/cache.rb', line 71 def update_cache(key, value) key = key.to_s localizations[key] = value end |
#update_locale(key, value, force_array = false, startup_process = false) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/lit/cache.rb', line 62 def update_locale(key, value, force_array = false, startup_process = false) key = key.to_s locale_key, key_without_locale = split_key(key) locale = find_locale(locale_key) localization = find_localization(locale, key_without_locale, value: value, force_array: force_array, update_value: true) return localization.get_value if startup_process && localization.is_changed? localizations[key] = localization.get_value if localization end |