Class: Tml::Cache

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

Instance Method Summary collapse

Instance Method Details

#cache_nameObject

name of the cache adapter



84
85
86
# File 'lib/tml/cache.rb', line 84

def cache_name
  self.class.name.split('::').last
end

#clear(opts = {}) ⇒ Object

clears cache



135
136
137
# File 'lib/tml/cache.rb', line 135

def clear(opts = {})
  # do nothing
end

#default_cache_pathObject

default cache path



230
231
232
233
234
235
236
237
238
# File 'lib/tml/cache.rb', line 230

def default_cache_path
  @cache_path ||= begin
    path = Tml.config.cache[:path]
    path ||= 'config/tml'
    FileUtils.mkdir_p(path)
    FileUtils.chmod(0777, path)
    path
  end
end

#delete(key, opts = {}) ⇒ Object

deletes key from cache



125
126
127
# File 'lib/tml/cache.rb', line 125

def delete(key, opts = {})
  # do nothing
end

#download(cache_path = default_cache_path, version = nil) ⇒ Object

downloads cache from the CDN



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/tml/cache.rb', line 241

def download(cache_path = default_cache_path, version = nil)
  t0 = Time.now
  Tml.logger = Logger.new(STDOUT)

  Tml.logger.debug('Starting cache download...')
  app = Tml::Application.new(key: Tml.config.application[:key], cdn_host: Tml.config.application[:cdn_host])
  extract_version(app, version)

  Tml.logger.debug("Downloading Version: #{Tml.cache.version}")

  archive_name = "#{Tml.cache.version}.tar.gz"
  path = "#{cache_path}/#{archive_name}"
  url = "#{app.cdn_host}/#{Tml.config.application[:key]}/#{archive_name}"

  Tml.logger.debug("Downloading cache file: #{url}")
  open(path, 'wb') do |file|
    file << open(url).read
  end

  Tml.logger.debug('Extracting cache file...')
  version_path = "#{cache_path}/#{Tml.cache.version}"
  Tml::Utils.untar(Tml::Utils.ungzip(File.new(path)), version_path)
  Tml.logger.debug("Cache has been stored in #{version_path}")

  File.unlink(path)

  begin
    current_path = 'current'
    FileUtils.chdir(cache_path)
    FileUtils.rm(current_path) if File.exist?(current_path)
    FileUtils.ln_s(Tml.cache.version.to_s, current_path)
    Tml.logger.debug("The new version #{Tml.cache.version} has been marked as current")
  rescue Exception => ex
    Tml.logger.debug("Could not generate current symlink to the cache path: #{ex.message}")
  end

  t1 = Time.now
  Tml.logger.debug("Cache download took #{t1-t0}s")
end

#enabled?Boolean

checks if Tml is enabled

Returns:

  • (Boolean)


73
74
75
# File 'lib/tml/cache.rb', line 73

def enabled?
  Tml.config.cache_enabled?
end

#exist?(key, opts = {}) ⇒ Boolean

checks if the key exists

Returns:

  • (Boolean)


130
131
132
# File 'lib/tml/cache.rb', line 130

def exist?(key, opts = {})
  false
end

#extract_version(app, version = nil) ⇒ Object

Pulls cache version from CDN



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/tml/cache.rb', line 140

def extract_version(app, version = nil)
  if version
    Tml.cache.version.set(version.to_s)
  else
    version_data = app.api_client.get_from_cdn('version', {t: Time.now.to_i}, {uncompressed: true})

    unless version_data
      Tml.logger.debug('No releases have been generated yet. Please visit your Dashboard and publish translations.')
      return
    end

    Tml.cache.version.set(version_data['version'])
  end
end

#fetch(key, opts = {}) ⇒ Object

fetches key from cache



114
115
116
117
# File 'lib/tml/cache.rb', line 114

def fetch(key, opts = {})
  return nil unless block_given?
  yield
end

#info(msg) ⇒ Object

logs information messages



89
90
91
# File 'lib/tml/cache.rb', line 89

def info(msg)
  Tml.logger.info("#{cache_name} - #{msg}")
end

#namespaceObject

namespace of each cache key



103
104
105
106
# File 'lib/tml/cache.rb', line 103

def namespace
  return '#' if Tml.config.disabled?
  @namespace || Tml.config.cache[:namespace] || Tml.config.application[:key][0..5]
end

#namespace=(value) ⇒ Object



98
99
100
# File 'lib/tml/cache.rb', line 98

def namespace=(value)
  @namespace = value
end

#read_only?Boolean

by default all cache is read/write cache like files based should be set to read only

Returns:

  • (Boolean)


79
80
81
# File 'lib/tml/cache.rb', line 79

def read_only?
  false
end

#reset_versionObject

resets current version



63
64
65
# File 'lib/tml/cache.rb', line 63

def reset_version
  version.reset
end

#store(key, data, opts = {}) ⇒ Object

stores key in cache



120
121
122
# File 'lib/tml/cache.rb', line 120

def store(key, data, opts = {})
  # do nothing
end

#strip_extensions(data) ⇒ Object

remove extensions



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/tml/cache.rb', line 282

def strip_extensions(data)
  if data.is_a?(Hash)
    data = data.dup
    data.delete('extensions')
    return data
  end

  if data.is_a?(String) and data.match(/^\{/)
    data = JSON.parse(data)
    data.delete('extensions')
    data = data.to_json
  end

  data
end

#upgrade_versionObject

upgrade current version



68
69
70
# File 'lib/tml/cache.rb', line 68

def upgrade_version
  version.upgrade
end

#versionObject

version object



58
59
60
# File 'lib/tml/cache.rb', line 58

def version
  @version ||= Tml::CacheVersion.new(self)
end

#versioned_key(key, opts = {}) ⇒ Object

versioned name of cache key



109
110
111
# File 'lib/tml/cache.rb', line 109

def versioned_key(key, opts = {})
  version.versioned_key(key, namespace)
end

#warmup(version = nil, cache_path = nil) ⇒ Object

Warms up cache from CDN or local files



156
157
158
159
160
161
162
# File 'lib/tml/cache.rb', line 156

def warmup(version = nil, cache_path = nil)
  if cache_path.nil?
    warmup_from_cdn(version)
  else
    warmup_from_files(version, cache_path)
  end
end

#warmup_from_cdn(version = nil) ⇒ Object

Warms up cache from CDN



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/tml/cache.rb', line 199

def warmup_from_cdn(version = nil)
  t0 = Time.now
  Tml.logger = Logger.new(STDOUT)

  Tml.logger.debug('Starting cache warmup from CDN...')
  app = Tml::Application.new(key: Tml.config.application[:key], cdn_host: Tml.config.application[:cdn_host])
  extract_version(app, version)

  Tml.logger.debug("Warming Up Version: #{Tml.cache.version}")

  application = app.api_client.get_from_cdn('application', {t: Time.now.to_i})
  Tml.cache.store(Tml::Application.cache_key, application)

  sources = app.api_client.get_from_cdn('sources', {t: Time.now.to_i}, {uncompressed: true})

  application['languages'].each do |lang|
    locale = lang['locale']
    language = app.api_client.get_from_cdn("#{locale}/language", {t: Time.now.to_i})
    Tml.cache.store(Tml::Language.cache_key(locale), language)

    sources.each do |src|
      source = app.api_client.get_from_cdn("#{locale}/sources/#{src}", {t: Time.now.to_i})
      Tml.cache.store(Tml::Source.cache_key(locale, src), source)
    end
  end

  t1 = Time.now
  Tml.logger.debug("Cache warmup took #{t1-t0}s")
end

#warmup_from_files(version = nil, cache_path = nil) ⇒ Object

Warms up cache from local files



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/tml/cache.rb', line 165

def warmup_from_files(version = nil, cache_path = nil)
  t0 = Time.now
  Tml.logger = Logger.new(STDOUT)

  Tml.logger.debug('Starting cache warmup from local files...')
  version ||= Tml.config.cache[:version]
  cache_path ||= Tml.config.cache[:path]
  cache_path = "#{cache_path}/#{version}"

  Tml.cache.version.set(version.to_s)
  Tml.logger.debug("Warming Up Version: #{Tml.cache.version}")

  application = JSON.parse(File.read("#{cache_path}/application.json"))
  Tml.cache.store(Tml::Application.cache_key, application)

  sources = JSON.parse(File.read("#{cache_path}/sources.json"))

  application['languages'].each do |lang|
    locale = lang['locale']

    language = JSON.parse(File.read("#{cache_path}/#{locale}/language.json"))
    Tml.cache.store(Tml::Language.cache_key(locale), language)

    sources.each do |src|
      source = JSON.parse(File.read("#{cache_path}/#{locale}/sources/#{src}.json"))
      Tml.cache.store(Tml::Source.cache_key(locale, src), source)
    end
  end

  t1 = Time.now
  Tml.logger.debug("Cache warmup took #{t1-t0}s")
end

#warn(msg) ⇒ Object

logs a warning



94
95
96
# File 'lib/tml/cache.rb', line 94

def warn(msg)
  Tml.logger.warn("#{cache_name} - #{msg}")
end