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
- #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
- #refresh_key(key) ⇒ Object
- #request_keys ⇒ Object
- #reset ⇒ Object (also: #clear)
- #reset_local_cache ⇒ Object
- #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
21 22 23 24 25 26 27 28 |
# 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 @localization_object_cache = {} @localization_key_object_cache = {} end |
Instance Method Details
#[](key) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/lit/cache.rb', line 30 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
39 40 41 |
# File 'lib/lit/cache.rb', line 39 def []=(key, value) update_locale(key, value) end |
#delete_key(key) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/lit/cache.rb', line 108 def delete_key(key) key = key.to_s localizations.delete(key) key_without_locale = split_key(key).last localization_keys.delete(key_without_locale) @localization_object_cache.delete(key) @localization_key_object_cache.delete(key_without_locale) I18n.backend.reload! end |
#delete_locale(key) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/lit/cache.rb', line 78 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) @localization_key_object_cache = {} @localization_object_cache = {} end |
#find_locale(locale_key) ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/lit/cache.rb', line 132 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
142 143 144 |
# File 'lib/lit/cache.rb', line 142 def get_global_hits_counter(key) @hits_counter['global_hits_counter.' + key] end |
#get_hits_counter(key) ⇒ Object
146 147 148 |
# File 'lib/lit/cache.rb', line 146 def get_hits_counter(key) @hits_counter['hits_counter.' + key] end |
#get_request_info(key_without_locale) ⇒ Object
337 338 339 |
# File 'lib/lit/cache.rb', line 337 def get_request_info(key_without_locale) @request_info_store['request_info.' + key_without_locale].to_s end |
#has_key?(key) ⇒ Boolean
47 48 49 |
# File 'lib/lit/cache.rb', line 47 def has_key?(key) localizations.has_key?(key) end |
#init_key_with_value(key, value) ⇒ Object
43 44 45 |
# File 'lib/lit/cache.rb', line 43 def init_key_with_value(key, value) update_locale(key, value, true) end |
#keys ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/lit/cache.rb', line 55 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
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/lit/cache.rb', line 88 def load_all_translations first = Localization.active.order(id: :asc).first last = Localization.active.order(id: :desc).first if !first || (!localizations.has_key?(first.full_key) || !localizations.has_key?(last.full_key)) Localization.includes(i[locale localization_key]).active.find_each do |l| localizations[l.full_key] = l.translation end end end |
#refresh_key(key) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/lit/cache.rb', line 99 def refresh_key(key) key = key.to_s locale_key, key_without_locale = split_key(key) locale = find_locale(locale_key) @localization_object_cache.delete(key) localization = find_localization(locale, key_without_locale, default_fallback: true) localizations[key] = localization.translation if localization end |
#request_keys ⇒ Object
332 333 334 |
# File 'lib/lit/cache.rb', line 332 def request_keys Thread.current[:lit_request_keys] || {} end |
#reset ⇒ Object Also known as: clear
124 125 126 127 128 129 |
# File 'lib/lit/cache.rb', line 124 def reset reset_local_cache localizations.clear localization_keys.clear load_all_translations end |
#reset_local_cache ⇒ Object
118 119 120 121 122 |
# File 'lib/lit/cache.rb', line 118 def reset_local_cache @locale_cache = {} @localization_key_object_cache = {} @localization_object_cache = {} end |
#restore_hits_counter ⇒ Object
154 155 156 |
# File 'lib/lit/cache.rb', line 154 def restore_hits_counter @hits_counter_working = true end |
#stop_hits_counter ⇒ Object
150 151 152 |
# File 'lib/lit/cache.rb', line 150 def stop_hits_counter @hits_counter_working = false end |
#sync ⇒ Object
51 52 53 |
# File 'lib/lit/cache.rb', line 51 def sync localizations.clear end |
#update_cache(key, value) ⇒ Object
73 74 75 76 |
# File 'lib/lit/cache.rb', line 73 def update_cache(key, value) key = key.to_s localizations[key] = value end |
#update_locale(key, value, force_array = false, startup_process = false) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/lit/cache.rb', line 64 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.translation if startup_process && localization.is_changed? localizations[key] = localization.translation if localization end |