Class: Lit::Cache

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

Constant Summary collapse

@@last_update =
nil
@@localization_cache =
{}

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lit/cache.rb', line 24

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lit/cache.rb', line 36

def [](key)
  value = nil
  check_and_clean_if_required
  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



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

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

#check_and_clean_if_requiredObject



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/lit/cache.rb', line 193

def check_and_clean_if_required
  # we want this method to run only once during a request or background job
  return if Thread.current[:localization_cache_valid]

  # get most recently updated translation and compare with what we store in class var
  last_up = Lit::Localization.order(updated_at: :desc).limit(1).pluck(:updated_at).first.to_i
  if last_up != @@last_update
    clear_localization_cache
    @@last_update = last_up
  end
  Thread.current[:localization_cache_valid] = true
end

#clear_localization_cacheObject



188
189
190
191
# File 'lib/lit/cache.rb', line 188

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

#delete_key(key) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/lit/cache.rb', line 131

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



99
100
101
102
103
104
105
106
107
108
# File 'lib/lit/cache.rb', line 99

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



157
158
159
160
161
162
163
164
165
# File 'lib/lit/cache.rb', line 157

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



167
168
169
# File 'lib/lit/cache.rb', line 167

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

#get_hits_counter(key) ⇒ Object



171
172
173
# File 'lib/lit/cache.rb', line 171

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

#get_request_info(key_without_locale) ⇒ Object



389
390
391
# File 'lib/lit/cache.rb', line 389

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

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#keysObject



73
74
75
76
77
78
79
80
# File 'lib/lit/cache.rb', line 73

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



110
111
112
113
114
115
116
117
118
119
# File 'lib/lit/cache.rb', line 110

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



183
184
185
186
# File 'lib/lit/cache.rb', line 183

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

#refresh_key(key) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/lit/cache.rb', line 121

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
  clear_localization_cache
end

#request_keysObject



384
385
386
# File 'lib/lit/cache.rb', line 384

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

#resetObject Also known as: clear



149
150
151
152
153
154
# File 'lib/lit/cache.rb', line 149

def reset
  reset_local_cache
  localizations.clear
  localization_keys.clear
  load_all_translations
end

#reset_local_cacheObject



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

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

#restore_hits_counterObject



179
180
181
# File 'lib/lit/cache.rb', line 179

def restore_hits_counter
  @hits_counter_working = true
end

#stop_hits_counterObject



175
176
177
# File 'lib/lit/cache.rb', line 175

def stop_hits_counter
  @hits_counter_working = false
end

#syncObject



69
70
71
# File 'lib/lit/cache.rb', line 69

def sync
  localizations.clear
end

#update_cache(key, value) ⇒ Object



93
94
95
96
97
# File 'lib/lit/cache.rb', line 93

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



82
83
84
85
86
87
88
89
90
91
# File 'lib/lit/cache.rb', line 82

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