Class: Lit::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/lit/cache.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



21
22
23
24
25
26
27
28
29
30
31
# 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 = Lit.hits_counter_enabled
  @keys = nil

  # current instance cache
  @localization_object_cache = {}
  @localization_key_object_cache = {}
  clear_localization_cache
end

Instance Method Details

#[](key) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lit/cache.rb', line 33

def [](key)
  value = nil
  unless localization_cache.key?(key)
    value = localizations[key]
    localization_cache[key] = value
  else
    value = localization_cache[key]
  end
  update_hits_count(key) if @hits_counter_working

  if Lit.store_request_info ||
     Lit.store_request_keys
    key_without_locale = split_key(key).last
    store_request_info(key_without_locale)
    update_request_keys(key_without_locale, value)
  end
  value
end

#[]=(key, value) ⇒ Object



52
53
54
# File 'lib/lit/cache.rb', line 52

def []=(key, value)
  update_locale(key, value)
end

#clear_localization_cacheObject



181
182
183
# File 'lib/lit/cache.rb', line 181

def clear_localization_cache
  Thread.current[:lit_thread_cache] = {}
end

#delete_key(key) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/lit/cache.rb', line 125

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



94
95
96
97
98
99
100
101
102
103
# File 'lib/lit/cache.rb', line 94

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 = {}
  clear_localization_cache
end

#find_locale(locale_key) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/lit/cache.rb', line 151

def find_locale(locale_key)
  locale_key = locale_key.to_s
  @locale_cache ||= {}
  unless @locale_cache.key?(locale_key) && @locale_cache[locale_key].id
    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



161
162
163
# File 'lib/lit/cache.rb', line 161

def get_global_hits_counter(key)
  @hits_counter['global_hits_counter.' + key]
end

#get_hits_counter(key) ⇒ Object



165
166
167
# File 'lib/lit/cache.rb', line 165

def get_hits_counter(key)
  @hits_counter['hits_counter.' + key]
end

#get_request_info(key_without_locale) ⇒ Object



368
369
370
# File 'lib/lit/cache.rb', line 368

def get_request_info(key_without_locale)
  @request_info_store['request_info.' + key_without_locale].to_s
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/lit/cache.rb', line 60

def has_key?(key)
  # check for instance cache first
  localization_cache.has_key?(key) || localizations.has_key?(key)
end

#init_key_with_value(key, value) ⇒ Object



56
57
58
# File 'lib/lit/cache.rb', line 56

def init_key_with_value(key, value)
  update_locale(key, value, true)
end

#keysObject



69
70
71
72
73
74
75
76
# File 'lib/lit/cache.rb', line 69

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_translationsObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/lit/cache.rb', line 105

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

#localization_cacheObject



177
178
179
# File 'lib/lit/cache.rb', line 177

def localization_cache
  Thread.current[:lit_thread_cache] ||= {}
end

#refresh_key(key) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/lit/cache.rb', line 116

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_keysObject



363
364
365
# File 'lib/lit/cache.rb', line 363

def request_keys
  Thread.current[:lit_request_keys] || {}
end

#resetObject Also known as: clear



143
144
145
146
147
148
# File 'lib/lit/cache.rb', line 143

def reset
  reset_local_cache
  localizations.clear
  localization_keys.clear
  load_all_translations
end

#reset_local_cacheObject



135
136
137
138
139
140
141
# File 'lib/lit/cache.rb', line 135

def reset_local_cache
  @locale_cache = {}
  @localization_key_object_cache = {}
  @localization_object_cache = {}
  @localization_cache = {}
  clear_localization_cache
end

#restore_hits_counterObject



173
174
175
# File 'lib/lit/cache.rb', line 173

def restore_hits_counter
  @hits_counter_working = true
end

#stop_hits_counterObject



169
170
171
# File 'lib/lit/cache.rb', line 169

def stop_hits_counter
  @hits_counter_working = false
end

#syncObject



65
66
67
# File 'lib/lit/cache.rb', line 65

def sync
  localizations.clear
end

#update_cache(key, value) ⇒ Object



88
89
90
91
92
# File 'lib/lit/cache.rb', line 88

def update_cache(key, value)
  key = key.to_s
  localizations[key] = value
  localization_cache[key] = value
end

#update_locale(key, value, force_array = false, startup_process = false) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/lit/cache.rb', line 78

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
  localization_cache[key] = localizations[key]
end