Class: Lit::Cache

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

Instance Method Summary collapse

Constructor Details

#initializeCache



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

#keysObject



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_translationsObject



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_keysObject



332
333
334
# File 'lib/lit/cache.rb', line 332

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

#resetObject 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_cacheObject



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_counterObject



154
155
156
# File 'lib/lit/cache.rb', line 154

def restore_hits_counter
  @hits_counter_working = true
end

#stop_hits_counterObject



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

def stop_hits_counter
  @hits_counter_working = false
end

#syncObject



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